Skip to content

Instantly share code, notes, and snippets.

@kuang-da
Last active October 25, 2020 20:19
Show Gist options
  • Save kuang-da/c05e03d47513cbca7d715c663d7fb2cc to your computer and use it in GitHub Desktop.
Save kuang-da/c05e03d47513cbca7d715c663d7fb2cc to your computer and use it in GitHub Desktop.
[Notes about R] Common used code snippet for R programming #R #datascience
p <- ggplot()
p <- ggplot(data = k_suggest_df,
aes(x = k, y = median_kl, col = calc))
p <- p + geom_line()
p <- p + xlab("K")
p <- p + ylab("Median KL divergence (across all cells)")
p <- p + theme_bw(base_size = 8)
#p <- p + theme(legend.key.size = unit(3, "line"))
p <- p + theme(legend.position = "bottom")
p <- p + theme(legend.title = element_blank())
p <- p + theme(legend.margin=margin(0,0,0,0))
p <- p + theme(legend.box.margin=margin(-10,0,0,0))
print(p)
library(dplyr)
df1 <- data.frame(x = 1:5, y = 2:6, z = 3:7)
rownames(df1) <- LETTERS[1:5]
df2 <- data.frame(x = 1:5, y = 2:6, z = 3:7)
rownames(df2) <- LETTERS[3:7]
# add rownames as a column in each data.frame and bind rows
bind_rows(df1 %>% tibble::rownames_to_column(),
df2 %>% tibble::rownames_to_column()) %>%
# evaluate following calls for each value in the rowname column
group_by(rowname) %>%
# add all non-grouping variables
summarise_all(sum)
@kuang-da
Copy link
Author

kuang-da commented Oct 10, 2020

Input format

> k_suggest_df
   median_kl  k    calc
1  0.2499575  5  KL_div
2  0.5786012 10  KL_div
3  0.7581971 15  KL_div
4  0.8712907 20  KL_div
5  0.9428609 25  KL_div
6  1.0332424 30  KL_div
7  1.0839087 35  KL_div
8  1.1635932 40  KL_div
9  1.2108593 45  KL_div
10 1.2550985 50  KL_div
11 2.3219281  5 log2(k)
12 3.3219281 10 log2(k)
13 3.9068906 15 log2(k)
14 4.3219281 20 log2(k)
15 4.6438562 25 log2(k)
16 4.9068906 30 log2(k)
17 5.1292830 35 log2(k)
18 5.3219281 40 log2(k)
19 5.4918531 45 log2(k)
20 5.6438562 50 log2(k)

@kuang-da
Copy link
Author

Output image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment