R GGPLOT平面条形图:多个“;回避;每种填充类型的钢筋?
我正在绘制此数据集“ds”R GGPLOT平面条形图:多个“;回避;每种填充类型的钢筋?,r,ggplot2,bar-chart,facet-grid,R,Ggplot2,Bar Chart,Facet Grid,我正在绘制此数据集“ds” ds <- structure(list(FN = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4
ds <- structure(list(FN = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L,
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("FN=1", "FN=2", "FN=3",
"FN=4"), class = "factor"), fraction = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("serum",
"plasma"), class = "factor"), demographics = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("not adjusted",
"adjusted"), class = "factor"), freq = c(132, 47, 14, 30, 29,
19, 25, 14, 9, 5, 4, 4, 24, 21, 15, 6, 6, 5, 4, 4, 2, 2, 2, 2,
35, 28, 25, 68, 24, 11, 33, 15, 10, 12, 11, 8, 24, 16, 15, 13,
10, 6, 7, 6, 5, 4, 3, 3)), .Names = c("FN", "fraction", "demographics",
"freq"), class = "data.frame", row.names = c(NA, -48L))
返回这个结果
如您所见,每个x变量“分数”(血清/血浆)和填充类别“人口统计”(未调整/调整)有三个条形图。但是,我希望每个填充类别的这三个条形图可以并排绘制。这可能吗
非常感谢你的帮助 添加单个分组变量
library("tidyverse")
ds %>% mutate(row = row_number()) %>%
ggplot(aes(x=fraction, y=freq, fill=factor(demographics, c("adjusted", "not adjusted")), group = row)) +
geom_bar(position=position_dodge(width=0.9), stat="identity", color="black") +
facet_grid(FN~., switch="y") +
scale_fill_manual("", values=c("not adjusted"="#e41a1c", "adjusted"="#377eb8"), guide=guide_legend(reverse = TRUE)) +
theme_bw() +
theme(legend.title=element_blank(), legend.position="bottom") +
theme(axis.title.x=element_text(face="bold")) +
theme(axis.title.y=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(panel.background=element_blank()) +
theme(panel.grid=element_blank()) +
theme(strip.text.y = element_text(angle=180)) +
coord_flip()
现在,在
人口统计数据中,列被减淡,但如果您再添加一个aestetic(在aes()
函数中)group=freq
ggplot将分别减淡每个条。因此,您的代码应为:
ggplot(ds, aes(x=fraction, y=freq, group=freq, fill=factor(demographics, c("adjusted", "not adjusted")))) +
geom_bar(position=position_dodge(width=0.9), stat="identity", color="black") +
facet_grid(FN~., switch="y") +
scale_fill_manual("", values=c("not adjusted"="#e41a1c", "adjusted"="#377eb8"), guide=guide_legend(reverse = TRUE)) +
theme_bw() +
theme(legend.title=element_blank(), legend.position="bottom") +
theme(axis.title.x=element_text(face="bold")) +
theme(axis.title.y=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(panel.background=element_blank()) +
theme(panel.grid=element_blank()) +
theme(strip.text.y = element_text(angle=180)) + coord_flip()
这看起来像:
感谢您快速准确的回答。还有一个问题,你知道我如何改变条形图的顺序,使“红色的”(未调整)在“蓝色的”之前/之上绘制吗?在scale\u fill\u manual
soscale\u fill\u manual(“,values=c”(“调整的”=“\377eb8”,“未调整的”=“\e41a1c”)中,首先放置你想要的那个…
听起来似乎有道理,但是,这对我不起作用。可能是因为翻转了绘图?请尝试Forcats中的fct_reorder()。这对当前数据集有效,但如果同一组中的两行具有相同的freq值,它将失败
ggplot(ds, aes(x=fraction, y=freq, group=freq, fill=factor(demographics, c("adjusted", "not adjusted")))) +
geom_bar(position=position_dodge(width=0.9), stat="identity", color="black") +
facet_grid(FN~., switch="y") +
scale_fill_manual("", values=c("not adjusted"="#e41a1c", "adjusted"="#377eb8"), guide=guide_legend(reverse = TRUE)) +
theme_bw() +
theme(legend.title=element_blank(), legend.position="bottom") +
theme(axis.title.x=element_text(face="bold")) +
theme(axis.title.y=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(panel.background=element_blank()) +
theme(panel.grid=element_blank()) +
theme(strip.text.y = element_text(angle=180)) + coord_flip()