Combine Stacked and Grouped chart ggplot2

只愿长相守 提交于 2021-01-28 11:06:24

问题


I have this table:

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

And I have generated the following plot in ggplot2, which is nearly what I want:

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)

Which generates a Figure like:

The problem is that I want the first bar of 60-15 to be splitted in two (1061 and 204) as if it was a stacked barchart. The remaining bars do not have this particularity and would remain the same as they are. I made an attempt to solve this by adding the rows with zero Number values, but I could not discover the proper code to solve this.

Could someone help?

Thanks


回答1:


You can try to calculate the sums before. Then add missing bar parts by your own using geom_rect

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)



来源:https://stackoverflow.com/questions/56750222/combine-stacked-and-grouped-chart-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!