我有 2 個資料框,如下所示:
df_hk_genes_pre = structure(list(ACTB = c(11.6704399, 12.458028, 11.200511, 12.3073524,
12.066374, 12.064411, 12.1516557, 8.669943, 12.045182, 12.35896,
11.3328069, 10.226411, 11.8971381, 12.0288182, 11.6919341, 12.0735249,
11.8812387, 11.8266266, 11.5526943, 12.3936434), ATP5F1 = c(8.6677137,
8.260138, 8.421619, 8.1465627, 8.956782, 8.792251, 8.6480966,
8.700314, 8.850915, 8.446602, 8.7311666, 8.762719, 8.1397597,
7.9228606, 8.909108, 8.8039817, 8.4693453, 8.5861887, 8.2678096,
8.7482762)), row.names = c(NA, 20L), class = "data.frame")
df_hk_genes_post = structure(list(ACTB = c(11.7087918, 13.1847403, 8.767737, 12.2949669,
12.399929, 12.130683, 9.816222, 10.700336, 11.862543, 12.479818,
12.48152, 11.798277, 12.0932696, 11.014992, 12.3496682, 11.9810211,
11.946094, 12.1517049, 11.6794028, 12.4895911), ATP5F1 = c(8.3731175,
8.3995189, 8.871088, 8.4389342, 8.529104, 9.004405, 8.883721,
8.70097, 8.24411, 8.393635, 8.76813, 8.756177, 8.4418168, 7.986864,
8.4840108, 8.6523954, 8.5645576, 8.2452877, 8.2440872, 8.7155973
)), row.names = c(NA, 20L), class = "data.frame")
我使用以下代碼分別為每個生成直方圖網格:
setDT(df_hk_genes_post)
melt(df_hk_genes_post) %>%
ggplot(aes(x = value))
facet_wrap(~ variable, nrow = 2, scale = "free")
geom_histogram(
fill="#69b3a2", color="#69b3a2", alpha=0.4, position="identity", bins=20
)
scale_x_continuous(
sec.axis = sec_axis(
~ . , name = "CPM of House-Keeping Genes Distribution",
breaks = NULL, labels = NULL
)
)
但現在我希望用不同的顏色在同一個網格上繪制兩者。是否可以使用相同的代碼段或應該采用不同的方法?
uj5u.com熱心網友回復:
您可以使用以下代碼。首先制作一個單獨的資料框,并使用額外的列指定哪個是 pre 哪個是 post。PrePost然后也生成一個刻面var的繪圖。
library(data.table)
## add column identifying pre or post: PrePost
## and rowbind together,
## make a factor from PrePost
df_hk_genes_pre$PrePost <- "pre"
df_hk_genes_post$PrePost <- "post"
df_hk_genes_all <- rbind(df_hk_genes_pre, df_hk_genes_post)
df_hk_genes_all$PrePost <- factor(df_hk_genes_all$PrePost)
## plot with facets in rows for "PrePost"
## and facets in columns for "variable"
setDT(df_hk_genes_all)
melt(df_hk_genes_all) %>%
ggplot(aes(x = value, fill = PrePost)) ### fill col based on PrePost
facet_grid(cols = vars(variable), rows = vars(PrePost)) ### PrePost in facet rows
geom_histogram(
bins=20
, color= "grey" ### visually distinct bars
)
scale_x_continuous(
sec.axis = sec_axis(
~ . , name = "CPM of House-Keeping Genes Distribution",
breaks = NULL, labels = NULL
)
)
這會產生以下圖表:

如果要更改填充顏色,可以添加類似于以下內容的行:
scale_fill_manual(values= c("#69b3a2", "#25a3c9"))
請讓我知道這是否是您的想法。
編輯 01
如果您想在同一個子圖上進行 pre 和 post,那么您可以將
position = "dodge"其用作引數geom_histogram()
setDT(df_hk_genes_all)
melt(df_hk_genes_all) %>%
ggplot(aes(x = value, fill = PrePost)) ### fill col based on PrePost
facet_grid(cols = vars(variable))
geom_histogram(
bins=20
, color= "grey", ### visually distinct bars
, position = "dodge" ### dodging
)
scale_x_continuous(
sec.axis = sec_axis(
~ . , name = "CPM of House-Keeping Genes Distribution",
breaks = NULL, labels = NULL
)
)
...產生這個情節:

uj5u.com熱心網友回復:
同樣,如果您想展示更緊密的重疊,您可以在同一個圖上組合兩個變數級別
df_hk_genes_pre2 = reshape::melt(df_hk_genes_pre) %>%
mutate(status = "pre")
df_hk_genes_post2 = reshape::melt(df_hk_genes_post) %>%
mutate(status = "post")
bind_rows(df_hk_genes_pre2, df_hk_genes_post2) %>%
mutate(status = factor(status, levels = c("pre", "post"))) %>%
ggplot(aes(x = value, fill = variable))
facet_wrap(~ status, nrow = 2, scale = "free")
geom_histogram(position = "identity", bins = 20, alpha = .4)
scale_fill_manual(values= c("#69b3a2", "black"))
scale_color_manual(values = c("#69b3a2", "black"))
scale_x_continuous(
sec.axis = sec_axis(
~ . , name = "CPM of House-Keeping Genes Distribution",
breaks = NULL, labels = NULL
)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416807.html
標籤:
上一篇:ggplot2之前/之后的斜率圖
