R 组合堆叠和分组图表ggplot2
我有这张桌子:R 组合堆叠和分组图表ggplot2,r,ggplot2,bar-chart,stacked,R,Ggplot2,Bar Chart,Stacked,我有这张桌子: 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
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中生成了以下绘图,这几乎就是我想要的:
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),就像它是一个堆叠条形图一样。其余的酒吧没有这种特殊性,将保持不变。我试图通过添加具有零数值的行来解决此问题,但我无法找到解决此问题的正确代码
有人能帮忙吗
谢谢您可以在之前尝试计算总和。然后使用
geom\u 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)
库(tidyverse)
df%>%
分组依据(类型、校正)%>%
总结(计数=总和(数字))
#一个tibble:12x3
#分组:类型[6]
类型更正计数
160-15更正1265
2 60-15本机540
3 60-30更正315
460-30本地人58
570-15更正200
6 70-15本地人61
7 70-30更正259
8 70-30本地人42
9 80-15更正166
10 80-15本地人76
11 80-30更正182
12 80-30本地人13
df%>%
分组依据(类型、校正)%>%
汇总(计数=总和(数字))%>%
突变(校正=系数(校正,水平=c(水平(校正),“新水平”))%>%
ggplot(aes(类型、计数、填充=校正))+
几何坐标(位置=“道奇”)+
几何校正(aes(xmin=0.55,xmax=1,ymin=1061,ymax=1061+204),fill=“#619CFF”)+
比例\填充\离散(下降=假)
您可以尝试在之前计算总和。然后使用
geom\u 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)
库(tidyverse)
df%>%
分组依据(类型、校正)%>%
总结(计数=总和(数字))
#一个tibble:12x3
#分组:类型[6]
类型更正计数
160-15更正1265
2 60-15本机540
3 60-30更正315
460-30本地人58
570-15更正200
6 70-15本地人61
7 70-30更正259
8 70-30本地人42
9 80-15更正166
10 80-15本地人76
11 80-30更正182
12 80-30本地人13
df%>%
分组依据(类型、校正)%>%
汇总(计数=总和(数字))%>%
突变(校正=系数(校正,水平=c(水平(校正),“新水平”))%>%
ggplot(aes(类型、计数、填充=校正))+
几何坐标(位置=“道奇”)+
几何校正(aes(xmin=0.55,xmax=1,ymin=1061,ymax=1061+204),fill=“#619CFF”)+
比例\填充\离散(下降=假)
我认为您的绘图是错误的,因为您的60-15校正条的最大值是1061,而不是(1061+204),对吗?是的,这是因为绘图以某种方式只占用第一行而不是第二行,我想您可以发布
dput(表)的输出吗
?我认为您的绘图是错误的,因为您的60-15校正条的最大值是1061,而不是(1061+204),对吗?是的,这是因为绘图以某种方式只占用第一行而不是第二行,我想您可以发布dput(表)
的输出吗?