r - 编辑使用 facet_wrap 绘制的每个面板的 x 轴标签
问题描述
你能帮我将中间 IQR 标签添加到由 facet_wrap 生成的面板的 x 轴吗?恐怕我无法复制以前的SO建议。我附上了我的代码和示例输出。谢谢。
#data
>head(df.tnbc,10)
# A tibble: 10 x 3
variable.x value.x value.y
<fct> <dbl> <fct>
1 Tumor_Fraction_ULPS 24.2 TNBC/basal (ER-/PR-/HER2-) +
2 Tumor_Fraction_30xWGS 24.3 TNBC/basal (ER-/PR-/HER2-) +
3 Tumor_Fraction_ULPS 13.2 TNBC/basal (ER-/PR-/HER2-) +
4 Tumor_Fraction_30xWGS 14.5 TNBC/basal (ER-/PR-/HER2-) +
5 Tumor_Fraction_ULPS 12.3 TNBC/basal (ER-/PR-/HER2-) +
6 Tumor_Fraction_30xWGS 11.7 TNBC/basal (ER-/PR-/HER2-) +
7 Tumor_Fraction_30xWGS 8.66 Non-TNBC/basal (ER-/PR-/HER2-)
8 Tumor_Fraction_ULPS 9.38 Non-TNBC/basal (ER-/PR-/HER2-)
9 Tumor_Fraction_ULPS 6.57 Non-TNBC/basal (ER-/PR-/HER2-)
10 Tumor_Fraction_30xWGS 7.61 Non-TNBC/basal (ER-/PR-/HER2-)
#code I tried
ggplot(data=df.tnbc ,aes(x=factor(value.y), y=value.x),color=value.x) +
geom_boxplot(outlier.size = 0,fill="darkgoldenrod2")+geom_jitter(shape=16, size=2,height=0)+
xlab("TNBC status by Immunohistochemistry staining") + ylab("ctDNA fractions / %")+scale_y_continuous(limits = c(0,25))+
ggtitle("ctDNA fractions by TNBC status") +
theme(plot.title=element_text(family="Times", face="bold", size=14,hjust = 1),
axis.text.x = element_text(family="Times", angle = 45, hjust = 1, face="bold"),
axis.text.y = element_text(family="Times", size = 14, face="bold"),
axis.title.y = element_text(family="Times", size = 14, face="bold"),
axis.title.x = element_text(family="Times", size = 14, face="bold"),
legend.title = element_text(family="Times",color = "black", size = 14, face="bold"),
legend.text = element_text(family="Times",color = "black", face="bold"))+stat_compare_means(na.rm=TRUE,label.x=3,hjust = 2.9)+
theme_pubclean() +
facet_wrap(~variable.x, scale="free")
##editing individual x- axis to append median IQR fails
ggplot(data=df.tnbc ,aes(x=factor(value.y), y=value.x),color=value.x) +
geom_boxplot(outlier.size = 0,fill="darkgoldenrod2")+geom_jitter(shape=16, size=2,height=0)+
xlab("TNBC status by Immunohistochemistry staining") + ylab("ctDNA fractions / %")+scale_y_continuous(limits = c(0,25))+
ggtitle("ctDNA fractions by TNBC status") +
theme(plot.title=element_text(family="Times", face="bold", size=14,hjust = 1),
axis.text.x = element_text(family="Times", angle = 45, hjust = 1, face="bold"),
axis.text.y = element_text(family="Times", size = 14, face="bold"),
axis.title.y = element_text(family="Times", size = 14, face="bold"),
axis.title.x = element_text(family="Times", size = 14, face="bold"),
legend.title = element_text(family="Times",color = "black", size = 14, face="bold"),
legend.text = element_text(family="Times",color = "black", face="bold"))+stat_compare_means(na.rm=TRUE,label.x=3,hjust = 2.9)+
theme_pubclean() +
facet_wrap(~variable.x, scale="free",
labeller = labeller(variable.x = c("Tumor_Fraction_ULPS","Tumor_Fraction_30xWGS"),
value.y = c("Non-TNBC/basal Med[IQR] 4.76% [1.12%-5.84%]","TNBC/basal (ER-/PR-/HER2-) Med[IQR] 12.8% [10.1%-15.9%]",
"Non-TNBC/basal Med[IQR] 3.08% [2.12%-6.33%]","TNBC/basal (ER-/PR-/HER2-) Med[IQR] 13.1% [9.41%-17.0%]")))
解决方案
尝试这种方法,但您必须隔离数据框中的标签并使用geom_text()
,因为我们不知道它们是如何计算的:
首先是标签:
library(ggpubr)
library(ggplot2)
#Data labels
dflab <- data.frame(variable.x=c(rep('Tumor_Fraction_30xWGS',2),
rep('Tumor_Fraction_ULPS',2)),
value.y=c('Non-TNBC/basal (ER-/PR-/HER2-)',
'TNBC/basal (ER-/PR-/HER2-) +',
'Non-TNBC/basal (ER-/PR-/HER2-)',
'TNBC/basal (ER-/PR-/HER2-) +'),
Lab=c('Med[IQR] 4.76% [1.12%-5.84%]',
'Med[IQR] 12.8% [10.1%-15.9%]',
'Med[IQR] 3.08% [2.12%-6.33%]',
'Med[IQR] 13.1% [9.41%-17.0%]'),
Coord=c(5,4,5,4),stringsAsFactors = F)
现在的情节:
#Plot
ggplot(data=df.tnbc ,aes(x=factor(value.y), y=value.x),color=value.x) +
geom_boxplot(outlier.size = 0,fill="darkgoldenrod2")+
geom_jitter(shape=16, size=2,height=0)+
xlab("TNBC status by Immunohistochemistry staining") +
ylab("ctDNA fractions / %")+scale_y_continuous(limits = c(0,25))+
ggtitle("ctDNA fractions by TNBC status") +
geom_text(data=dflab,
aes(x=factor(value.y), y=Coord,label=Lab))+
theme(plot.title=element_text(family="Times", face="bold", size=14,hjust = 1),
axis.text.x = element_text(family="Times", angle = 45, hjust = 1, face="bold"),
axis.text.y = element_text(family="Times", size = 14, face="bold"),
axis.title.y = element_text(family="Times", size = 14, face="bold"),
axis.title.x = element_text(family="Times", size = 14, face="bold"),
legend.title = element_text(family="Times",color = "black", size = 14, face="bold"),
legend.text = element_text(family="Times",color = "black", face="bold"))+stat_compare_means(na.rm=TRUE,label.x=3,hjust = 2.9)+
theme_pubclean() +
facet_wrap(~variable.x, scale="free")
输出:
推荐阅读
- html - 使用 Handlebars 隐藏过滤项目的空列表
- typescript - TypeScript:如何创建一个类型或接口,它是一个具有 n 个键之一的对象还是一个字符串?
- mongodb - 如何在ubuntu启动后启动mongo db
- node.js - NodeJS-Electron-msnodesqlv8 - 在 msnodesqlv8 中加载二进制文件时出错
- pointers - 队列实现抛出不兼容的指针类型错误
- c# - 在给定时间内执行任务
- php - 当页面不存在时不要创建错误页面
- delphi - 如何使用 TRttiProperty.setValue() 设置“记录”值?
- javascript - Google 代码美化代码突出显示不适用于 Polymer 3
- sql - SQL 计数显示不同的名称,但不正确的匹配数