条形图中仅丢失一个条 [英] Missing only one bar in bar-chart

查看:73
本文介绍了条形图中仅丢失一个条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggplot处理重复的条形图,并且几乎可以得到所需的内容,除了无法解决的错误.

I'm working on a duplicate bar chart using ggplot and I'm almost getting what I want, except for this error I couldn't fix.

我正在绘制的数据:

      comp     reg counts
1   C0xC12      up    569
2   C0xC12    down    845
3   C0xC24      up    420
4   C0xC24    down    227
5   C0xT12      up   2904
6   C0xT12    down   2989
7   C0xT24      up   2536
8   C0xT24    down   2309
9  C12xC24      up    314
10 C12xC24    down    138
11 C12xT12      up   3006
12 C12xT12    down   3082
13 C12xT24      up   2795
14 C12xT24    down   2577
15 C24xT12      up   1381
16 C24xT12    down   1901
17 C24xT24      up    482
18 C24xT24    down    862
19 T12xT24      up   1732
20 T12xT24    down   1464
21  C0xC12   uppcw      7
22  C0xC12 downpcw     15
23  C0xC24   uppcw     10
24  C0xC24 downpcw      4
25  C0xT12   uppcw     56
26  C0xT12 downpcw     58
27  C0xT24   uppcw     49
28  C0xT24 downpcw     41
29 C12xC24   uppcw      8
30 C12xC24 downpcw      2
31 C12xT12   uppcw     64
32 C12xT12 downpcw     50
33 C12xT24   uppcw     48
34 C12xT24 downpcw     39
35 C24xT12   uppcw     35
36 C24xT12 downpcw     40
37 C24xT24   uppcw     11
38 C24xT24 downpcw     15
39 T12xT24   uppcw     33
40 T12xT24 downpcw     30

我正在运行的代码:

teste <- read.table("teste.txt", sep = "\t", header = TRUE)
ggplot(data = teste, 
       mapping = aes(x = comp, fill = reg,
                     y = ifelse(test = reg == "down" | reg == "downpcw", 
                                yes = -counts, no = counts))) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = abs, limits = max(teste$counts) * c(-1,1)) +
  geom_text(aes(label=counts), vjust=0.5, color="black", size=3.0, nudge_y = c(-130,130))+
  labs(y = "DEGs", x = "Groups comparisons") + scale_fill_manual(values=c("#98FB98","#FA8072","#00FF00","#FF0000")) +
  scale_x_discrete(limits=c("T12xT24", "C24xT24", "C24xT12", "C12xT24", "C12xT12", "C12xC24", "C0xT24", "C0xT12", "C0xC24","C0xC12")) +
  coord_flip()

这就是我要得到的情节

请注意,"C12xT12"最大的条形图并未显示,只是其大小(3082).

notice that "C12xT12" biggest bar is not showing, just its size (3082).

我一直在寻找解决方案,但一无所获,我无法独自提出一个解决方案,所以我问是否有人曾经处理过此问题.

I've looked for solutions but found nothing and I can't come up with one alone, so I'm asking if anyone has ever dealt with this issue before.

推荐答案

问题来自您的 scale_y_continuous .您将其设置为 counts 列的最大值,该列将截断max列,因为它恰好位于图的极限处.如果您将最大值乘以1.05(而不是1),那么一切都会正常.
scale_y_continuous 的相应部分更改为: limits = max(teste $ counts)* c(-1.05,1.05).

The problem comes from your scale_y_continuous. You're setting it to the max of the counts column which cuts off the max column as it's exactly at the limit of the plot. If you multiply the max with 1.05 instead of 1, everything should work.
Change the corresponding part of the scale_y_continuous to: limits = max(teste$counts) * c(-1.05,1.05).

还有一些最佳做法的颜色建议:避免使用红绿色组合,因为约10%的男人是色盲的,无法分辨出这些颜色的阴影.

Also some best practice colour advice: avoid using red-green combination as ~10% of men are colourblind and cannot tell shades of these colours apart.

这篇关于条形图中仅丢失一个条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆