[英]ggplot2: geom_bar(); how to alternate order of fill so bars are not lost inside a bar with a higher value?
我試圖將兩個條形放置在 x 軸上的相同位置並按顏色分開(幾乎就像堆疊一樣)。
但是,我不希望將條形堆疊在另一個條形內 - 最小的 Y 值在具有最高 Y 值的條形內可見。
我可以在某種程度上讓它發揮作用 - 但問題是一個 Y 值在兩個因素之一中並不總是更高。 這會導致條形在 Y 值較高的條形中“丟失”。
這是我的數據集的一個子集和當前的 ggplot 代碼:
condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594
sf_plot <- ggplot(data = gene_freq,
aes(x = hours,
y = freq_genes,
group = condition,
fill = factor(expression,
labels=c("Down",
"Up"))))
sf_plot <- sf_plot + labs(fill="Expression")
sf_plot <- sf_plot + geom_bar(stat = "identity",
width = 2.5,
position = "dodge")
sf_plot <- sf_plot + scale_fill_manual(values=c("#9ecae1",
"#3182bd"))
sf_plot <- sf_plot + xlab("Time (Hours)")
sf_plot <- sf_plot + scale_x_continuous(breaks =
seq(min(gene_freq$freq_genes),
max(gene_freq$freq_genes),
by = 3))
sf_plot <- sf_plot + ylab("Gene Frequency")
sf_plot <- sf_plot + facet_grid(. ~ condition, scales = "free")
sf_plot <- sf_plot + theme_bw()
sf_plot <- sf_plot + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
sf_plot <- sf_plot + theme(axis.text.x = element_text(angle = 90))
# Print plot
sf_plot
您可以將alpha = 0.5
添加到geom_bar()
語句以使條形透明。 這將允許看到兩個條。 添加該alpha
語句和其他任何內容都不會產生您正在尋找的內容,以使兩個重疊的條都可見。 然而,顏色使得看到兩個不同的酒吧具有挑戰性。
另一個(也許更好)的選擇是更改創建繪圖的順序。 如果我沒ggplot
話, ggplot
將按字母或數字或因子級別的順序繪制條形圖。 在這里,您的expression
值為c("Down", "Up")
並且首先繪制了"Down"
。 如果您強制先繪制"Up"
,您也可以解決此問題。
library(dplyr)
library(ggplot2)
dat <-
read.table(text = "condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594") %>%
mutate(expression2 = ifelse(expression == "up", 1, 2))
dat %>%
ggplot(aes(x = hours, y = freq_genes, group = condition,
fill = factor(expression2, labels=c("Up", "Down")))) +
labs(fill="Expression") +
geom_bar(stat = "identity", position = "dodge", width = 2.5, alpha = 0.5) +
scale_fill_manual(values=c("#9ecae1", "#3182bd")) +
xlab("Time (Hours)") +
scale_x_continuous(breaks = seq(min(dat$freq_genes),
max(dat$freq_genes),
by = 3)) +
ylab("Gene Frequency") +
facet_grid(. ~ condition, scales = "free") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom",
axis.text.x = element_text(angle = 90))
在這里,我創建了一個新列名為expression2
是只是一個數字版本expression
。 我更改了aes()
的fill
變量以匹配那些新標簽。 我在scale_fill_manual()
中保留了與原始語句中相同的顏色並保留了alpha
值。 “向下”被繪制在“向上”的頂部,但為了與alpha
值保持相同的顏色,兩個條形更容易看到。 如果有必要,您可以使用圖例在“向上”之前顯示“向下”。
請注意,提供機器可讀數據對於讓其他人幫助您大有幫助。 考慮使用dput()
下次輸出您的數據而不是粘貼它。另請注意,您可以使用+
將ggplot()
語句“鏈接”在一起。 這使得代碼更加緊湊和易於閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.