首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并堆积和分组的图表ggplot2

合并堆积和分组的图表ggplot2
EN

Stack Overflow用户
提问于 2019-06-25 16:53:42
回答 1查看 55关注 0票数 1

我有一张桌子:

代码语言:javascript
复制
Number  Type    Correction  Adjust  Origin
1061    60-15   Corrected   yes     Small RNA-seq
204     60-15   Corrected   no      Small RNA-seq
0       60-15   Native      yes     Small RNA-seq
540     60-15   Native      no      Small RNA-seq
0       60-30   Corrected   yes     Small RNA-seq
315     60-30   Corrected   no      Small RNA-seq
0       60-30   Native      yes     Small RNA-seq
58      60-30   Native      no      Small RNA-seq
0       70-15   Corrected   yes     Small RNA-seq
200     70-15   Corrected   no      Small RNA-seq
0       70-15   Native      yes     Small RNA-seq
61      70-15   Native      no      Small RNA-seq
0       70-30   Corrected   yes     Small RNA-seq
259     70-30   Corrected   no      Small RNA-seq
0       70-30   Native      yes     Small RNA-seq
42      70-30   Native      no      Small RNA-seq
0       80-15   Corrected   yes     Small RNA-seq
166     80-15   Corrected   no      Small RNA-seq
0       80-15   Native      yes     Small RNA-seq
76      80-15   Native      no      Small RNA-seq
0       80-30   Corrected   yes     Small RNA-seq
182     80-30   Corrected   no      Small RNA-seq
0       80-30   Native      yes     Small RNA-seq
13      80-30   Native      no      Small RNA-seq

并且我已经在ggplot2中生成了以下图,这几乎就是我想要的:

代码语言:javascript
复制
ggplot(Table, aes(fill=Correction, x=Type, y=Number)) +
       geom_bar(position="dodge", stat="identity") +
       scale_fill_brewer(palette = "Set1") + labs(x="", y="") + 
       theme(legend.title=element_blank()) + facet_wrap(~Origin)

它会生成如下图:

问题是我希望60-15的第一个条形图被一分为二(1061和204),就像它是一个堆叠的条形图一样。其余的条形图没有这种特殊性,将保持不变。我试图通过添加具有零数值的行来解决这个问题,但是我找不到正确的代码来解决这个问题。

有人能帮帮忙吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-25 17:11:10

您可以尝试在此之前计算总和。然后使用geom_rect自行添加缺少的条形图部分

代码语言:javascript
复制
library(tidyverse)
df %>% 
  group_by(Type, Correction) %>% 
   summarise(count=sum(Number))
# A tibble: 12 x 3
# Groups:   Type [6]
Type  Correction count
<fct> <fct>      <int>
1 60-15 Corrected   1265
2 60-15 Native       540
3 60-30 Corrected    315
4 60-30 Native        58
5 70-15 Corrected    200
6 70-15 Native        61
7 70-30 Corrected    259
8 70-30 Native        42
9 80-15 Corrected    166
10 80-15 Native        76
11 80-30 Corrected    182
12 80-30 Native        13

df %>% 
  group_by(Type, Correction) %>% 
  summarise(count=sum(Number)) %>% 
  mutate(Correction=factor(Correction, levels = c(levels(Correction), "new_level"))) %>%  
  ggplot(aes(Type, count, fill=Correction)) + 
   geom_col(position = "dodge") +
   geom_rect(aes(xmin=.55, xmax=1, ymin=1061, ymax=1061+204), fill="#619CFF") + 
   scale_fill_discrete(drop=FALSE)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56750222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档