简体   繁体   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?

I am trying to position two bars at the same position on the x-axis and seperated out by colour (almost as if stacking).我试图将两个条形放置在 x 轴上的相同位置并按颜色分开(几乎就像堆叠一样)。

However, instead of stacking I want the bar simply inside the other bar - with the smallest Y-value being visable inside the bar with the highest Y-value.但是,我不希望将条形堆叠在另一个条形内 - 最小的 Y 值在具有最高 Y 值的条形内可见。

I can get this to work to some extent - but the issue is that one Y-value is not consistently higher across one of the two factors.我可以在某种程度上让它发挥作用 - 但问题是一个 Y 值在两个因素之一中并不总是更高。 This leads to bars being 'lost' within a bar with a higher Y-value.这会导致条形在 Y 值较高的条形中“丢失”。

Here is a subset of my dataset and the current ggplot code:这是我的数据集的一个子集和当前的 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

在此处输入图片说明

You can add alpha = 0.5 to your geom_bar() statement to make the bars transparent.您可以将alpha = 0.5添加到geom_bar()语句以使条形透明。 This will allow both bars to be seen.这将允许看到两个条。 Adding that alpha statement and nothing else will produce what you're looking for, to make both overlaid bars visible.添加该alpha语句和其他任何内容都不会产生您正在寻找的内容,以使两个重叠的条都可见。 The colors, however, make seeing the two different bars challenging.然而,颜色使得看到两个不同的酒吧具有挑战性。

在此处输入图片说明

Another (and maybe better) option is to change the order in which the plot is created.另一个(也许更好)的选择是更改创建绘图的顺序。 If I recall correctly, ggplot will plot the bars in alphabetical or numeric or factor-level order.如果我没ggplot话, ggplot将按字母或数字或因子级别的顺序绘制条形图。 Here, your expression values are c("Down", "Up") and "Down" is being plotted first.在这里,您的expression值为c("Down", "Up")并且首先绘制了"Down" If you force "Up" to be plotted first, you could resolve this, too.如果您强制先绘制"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))

Here, I've created a new column called expression2 that is just a numeric version of expression .在这里,我创建了一个新列名为expression2是只是一个数字版本expression I changed the fill variable in aes() to match with those new labels.我更改了aes()fill变量以匹配那些新标签。 I left the colors in scale_fill_manual() the same as in your original statement and kept the alpha value.我在scale_fill_manual()中保留了与原始语句中相同的颜色并保留了alpha值。 "Down" is being plotted on top of "Up" but in keeping the same colors with the alpha value, both bars are easier to see. “向下”被绘制在“向上”的顶部,但为了与alpha值保持相同的颜色,两个条形更容易看到。 You can play with the legend to display "Down" before "Up" if that's necessary.如果有必要,您可以使用图例在“向上”之前显示“向下”。

在此处输入图片说明

Note that providing machine readable data goes a long way in allowing others to help you out.请注意,提供机器可读数据对于让其他人帮助您大有帮助。 Consider using dput() to output your data next time rather than pasting it in. Also note that you can "chain" together ggplot() statements with a + .考虑使用dput()下次输出您的数据而不是粘贴它。另请注意,您可以使用+ggplot()语句“链接”在一起。 This makes code much more compact and easier to read.这使得代码更加紧凑和易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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