首页 > 解决方案 > 编辑使用 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%]")))

标签不会改变 在此处输入图像描述

标签: rggplot2facet-wrap

解决方案


尝试这种方法,但您必须隔离数据框中的标签并使用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")

输出:

在此处输入图像描述


推荐阅读