使用从这里生成的数据,我想让 ggplot2 创建一个类似于下面的图。
带有不良标签 ( facet_wrap
) 的理想绘图面板:
此图中的目的是允许在第一列 (Approach1) 的分布平均值与每列的密度平均值之间进行视觉比较。创建密度图的脚本如下:
ggplot(surg_df, aes(x=op_tm, col=color_hex)) +
geom_density(aes(fill=color_hex), alpha=0.3) +
geom_density(data = base_comp_df, col='#a269ff', alpha=1, size=0.5) +
geom_vline(data=avg_surg_df, aes(xintercept= avg_op_tm),
size=1, col=avg_surg_df$color_hex, linetype="dashed") +
geom_vline(data=avg_comp_df, aes(xintercept= avg_op_tm+2),
size=1, colour='#a269ff', linetype="dashed") +
annotate(geom= "text",
label=paste("mean diff: ",
as.character(floor(avg_comp_df$avg_op_tm-avg_surg_df$avg_op_tm)),
sep=""),
col='black', size=4, x=100, y=0.006) +
geom_segment(aes(x = avg_op_tm, y=0.006, xend = avg_surg_df$avg_op_tm,
yend = 0.006, colour = "red") ,
size=1, data = avg_comp_df) +
facet_wrap(~surg_grp) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_colour_identity(guide="none", breaks=base_surg_df$color_hex) +
scale_fill_identity(guide="legend", breaks=base_surg_df$color_hex,
name="Surgical Approaches",
labels=base_surg_df$surg_apprch)
最好使用facet_wrap()
with grouping by variable创建上图surg_grp
。但是当我决定利用标签的优势让它看起来不那么凌乱时facet_grid()
,事情就失控了。到目前为止,我设法创建的facet_grid()
是以下糟糕的密度图集合,每个面板都有 3 条可笑的avg_op_tm
垂直avg_surg_df
线.
不受欢迎的绘图面板,理想的标签(facet_grid
):
正如您在下面的脚本中注意到的那样,与上一个图不同,只有一个 geom_vline 并且每个面板上的三行来自该单行:
ggplot(surg_df)+
geom_density(aes(x=op_tm, col=color_hex, fill=color_hex), alpha=0.3) +
scale_fill_identity("Approaches", guide="legend", breaks=base_surg_df$color_hex,
labels=base_surg_df$surg_apprch,
aesthetics = "fill")+
scale_colour_identity(guide="none",breaks=base_surg_df$color_hex)+
geom_density(data = base_comp_df, aes(x=op_tm), alpha=1, col='#a269ff', size=0.5) +
geom_vline(data=avg_surg_df, aes(xintercept= avg_op_tm), size=1,
linetype="dashed")+
annotate(geom= "text",
label=paste("mean diff: ",
as.character(floor(avg_surg_df$avg_op_tm)), sep=""),
col='black', size=4, x=100, y=0.006)+
facet_grid(rows = vars(condition_grp), cols=vars(surg_apprch), scales = 'free')
社区有很多类似的问答,facet_wrap() + geom_vline()
但关于facet_grid() + geom_vline()
. 如何geom_vline()
使用已输入facet_wrap
(condition_grp
和surg_apprch
) 的两个分组参数并使其正确映射数据?我没有学到什么教育点导致我的方法facet_grid()
失败了?
非常感谢任何帮助。
更新
> head(avg_comp_df)
surg_grp avg_op_tm Cnt surg_apprch color_hex
1 A1 309.5494 74 Approach1 #a269ff
2 A2 309.5494 74 Approach2 #00CC00
3 A3 309.5494 74 Approach3 #FFAA93
4 A4 309.5494 74 Approach4 #5DD1FF
5 B1 263.0835 71 Approach1 #a269ff
6 B2 263.0835 71 Approach2 #00CC00
> head(surg_df) #used to create 12 different curves
surg_grp surg_apprch condition_grp op_tm color_hex
1 A1 Approach1 Benign-1 287.2103 #a269ff
2 A1 Approach1 Benign-1 261.2655 #a269ff
3 A1 Approach1 Benign-1 308.9267 #a269ff
4 A1 Approach1 Benign-1 257.9060 #a269ff
5 A1 Approach1 Benign-1 408.0310 #a269ff
6 A1 Approach1 Benign-1 405.4334 #a269ff
> head(avg_surg_df)
surg_grp avg_op_tm Cnt surg_apprch color_hex
1 A1 309.5494 74 Approach1 #a269ff
2 A2 378.4466 113 Approach2 #00CC00
3 A3 242.9890 101 Approach3 #FFAA93
4 A4 273.0774 71 Approach4 #5DD1FF
5 B1 263.0835 71 Approach1 #a269ff
6 B2 243.1910 85 Approach2 #00CC00
> head(base_comp_df) #to create similar orchid control distributions in each row
surg_grp surg_apprch condition_grp op_tm color_hex
1 A1 Approach1 Benign-1 287.2103 #a269ff
2 A1 Approach1 Benign-1 261.2655 #a269ff
3 A1 Approach1 Benign-1 308.9267 #a269ff
4 A1 Approach1 Benign-1 257.9060 #a269ff
5 A1 Approach1 Benign-1 408.0310 #a269ff
6 A1 Approach1 Benign-1 405.4334 #a269ff
> head(base_surg_df) #to make the legend
surg_apprch condition_grp surg_grp color_hex
1 Approach1 Benign-1 A1 #a269ff
2 Approach2 Benign-1 A2 #00CC00
3 Approach3 Benign-1 A3 #FFAA93
4 Approach4 Benign-1 A4 #5DD1FF
5 Approach1 Benign-2 B1 #a269ff
6 Approach2 Benign-2 B2 #00CC00