(编辑:这篇文章中提出的问题在下面解决)
我试图让我的条形图看起来像某种方式。我希望您可以在不真正解释此图的目的或数据背景的情况下关注我,因此我将直接进入。
我正在使用此代码
ggplot(ds, aes(x=fraction, y=AUC)) +
geom_bar(aes(fill=factor(demographics, c("adjusted", "not adjusted"))), position=position_dodge(width=0.9), stat="identity") +
facet_grid(~FN, switch="x") +
geom_text(aes(label=round(AUC, 2), fontface="bold", vjust=-0.4), position=position_dodge(width=0.9), size=2.75) +
theme(legend.title=element_blank(), legend.position="bottom",
axis.text.y=element_blank(),
axis.title.x=element_blank(), axis.ticks.x=element_blank(), axis.ticks.y=element_blank(),
panel.background=element_blank())
产生以下情节
然而,我想看到的是
条形顶部的 AUC 值与每个条形正确对齐(按“人口统计”[调整/未调整]分组),而不是与组“分数”(血清/血浆)的中间对齐,因为它是正确的现在
最后一个刻面“FN=5”中的两个条形各只有其当前宽度的一半(以匹配刻面“FN=1”到“FN=4”中其他条形的宽度
在每个条中垂直打印另一个标签。此标签存储在用于打印绘图的数据集“ds”中包含的字符变量“features”中
ds <- structure(list(ds = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4"), class = "factor"), FN = structure(c(1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
5L), .Label = c("FN=1", "FN=2", "FN=3", "FN=4", "FN=5"), class = "factor"),
fraction = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("serum",
"plasma"), class = "factor"), demographics = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L), .Label = c("not adjusted", "adjusted (age, sex, education, ethnicity, ApoE4)"
), class = "factor"), AUC = c(0.741264171455195, 0.749961174095356,
0.797639384997671, 0.763006678055599, 0.894704146606616,
0.9038670601025, 0.90169280944246, 0.912564062742662, 0.912564062742662,
0.672077922077922, 0.715367965367965, 0.746753246753247,
0.791125541125541, 0.804112554112554, 0.827922077922078,
0.829004329004329, 0.83982683982684, 0.83982683982684, 0.741264171455195,
0.749961174095356, 0.797639384997671, 0.763006678055599,
0.894704146606616, 0.9038670601025, 0.90169280944246, 0.912564062742662,
0.912564062742662, 0.672077922077922, 0.715367965367965,
0.746753246753247, 0.791125541125541, 0.804112554112554,
0.827922077922078, 0.829004329004329, 0.83982683982684, 0.83982683982684
), features = c("p21", "p3 + p21", "p3 + p7 + p19", "p1 + p8 + p21 + p23",
"p8", "p11 + p24", "p14 + p17 + p24", "p11 + p13 + p14 + p17",
"p11 + p13 + p14 + p17", "p13", "p9 + p17", "p9 + p14 + p17",
"p7 + p9 + p17 + p19", "p20", "p9 + p19", "p9 + p17 + p19",
"p4 + p8 + p9 + p17", "p4 + p8 + p9 + p17", "p21", "p3 + p21",
"p3 + p7 + p19", "p1 + p8 + p21 + p23", "p8", "p11 + p24",
"p14 + p17 + p24", "p11 + p13 + p14 + p17", "p11 + p13 + p14 + p17",
"p13", "p9 + p17", "p9 + p14 + p17", "p7 + p9 + p17 + p19",
"p20", "p9 + p19", "p9 + p17 + p19", "p4 + p8 + p9 + p17",
"p4 + p8 + p9 + p17")), .Names = c("ds", "FN", "fraction",
"demographics", "AUC", "features"), row.names = c(NA, -36L), class = "data.frame")
编辑:最终,使用评论部分的帮助,我使用了这段代码
ggplot(ds, aes(x=fraction, y=AUC, fill=factor(demographics, c("adjusted (age, sex, education, ethnicity, ApoE4)", "not adjusted")))) +
geom_bar(position=position_dodge(width=0.9), stat="identity") +
facet_grid(~FN) +
geom_text(aes(label=round(AUC, 2), fontface="bold", vjust=1), position=position_dodge(width=0.9), size=2.75) +
geom_text(aes(y=0.3, label=features, fontface="bold"), color="white", position=position_dodge(width=0.9), angle=90, size=3) +
theme_bw() +
theme(legend.title=element_blank(), legend.position="bottom",
axis.text.y=element_blank(),
axis.title.x=element_blank(), axis.title.y=element_text(face="bold"), axis.ticks.x=element_blank(), axis.ticks.y=element_blank(),
panel.background=element_blank(), panel.grid.minor=element_blank(), panel.grid.major=element_blank())
产生我最初想要的结果(我改变了主意,将刻面“FN = 5”中的列宽度减半),看起来像这样
为了提高可读性,我决定使用此代码翻转情节
ggplot(ds, aes(x=fraction, y=AUC, fill=factor(demographics, c("not adjusted", "adjusted (age, sex, education, ethnicity, ApoE4)")))) +
geom_bar(position=position_dodge(width=0.9), stat="identity") +
facet_grid(FN~.) +
geom_text(aes(y=AUC, label=round(AUC, 2), fontface="bold"), position=position_dodge(width=0.9), hjust=1.15, size=3.25) +
geom_text(aes(y=0.4, label=features, fontface="bold"), position=position_dodge(width=0.9), color="white", size=3) +
theme_bw() +
theme(legend.title=element_blank(), legend.position="bottom",
axis.title.x=element_text(face="bold"), axis.title.y=element_blank(),
axis.ticks.x=element_blank(), axis.ticks.y=element_blank(),
panel.background=element_blank(), panel.grid.minor=element_blank(), panel.grid.major=element_blank(),
strip.text.y = element_text(angle=0)) + coord_flip()
这导致水平条形图现在看起来像这样(图例键的颜色和顺序还不太正确)