繁体   English   中英

ggplot2: geom_bar(); 如何交替填充顺序,使条形不会在具有较高值的​​条形内丢失?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM