R 如何在ggboxplot中更改x轴标签

R 如何在ggboxplot中更改x轴标签,r,ggplot2,R,Ggplot2,我有一个包含多个因素的数据框架。我使用ggboxplot得到一个方框图,其中包含不同类别的比较。我对x轴标签不满意。我尝试了不同的方法,但没有达到我的预期。 用于创建绘图的代码为: df <- data.frame(country=sample(LETTERS[1:4], 1000, TRUE), rating=round(rnorm(1000,70,15),1), sex =rep(c("Female","Male"),500), school=sampl

我有一个包含多个因素的数据框架。我使用ggboxplot得到一个方框图,其中包含不同类别的比较。我对x轴标签不满意。我尝试了不同的方法,但没有达到我的预期。 用于创建绘图的代码为:

    df <- data.frame(country=sample(LETTERS[1:4], 1000, TRUE),
    rating=round(rnorm(1000,70,15),1),
    sex =rep(c("Female","Male"),500),
    school=sample(c("public","private"),1000,TRUE))
    df$group <- paste(df$school,df$sex,sep=".")
    df <- df[order(df$group),]
    my_comparisons <- list(c("public.Female","public.Male") , c("private.Female","private.Male"))
    library(ggpubr)
    ggboxplot(df, x = "group",y = "rating",
          color = "group", palette = "simpsons",
          add = "jitter",facet.by="country",legend="none", ylab="Rating") +
      theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
        axis.text.x = element_text(angle = 45, hjust = 1),
        axis.title.x = element_blank()) +
      stat_compare_means(method = "t.test",comparisons = my_comparisons,
                 label.y = 110,label = "p.signif")

df这使您接近您要查找的内容(我无法找出行分隔符)。您可能还需要调整标签的位置,以使其正确,以及尺寸

ggboxplot(df, x = "group",y = "rating",
          color = "group", palette = "simpsons",
          add = "jitter", facet.by="country", legend="none", ylab="Rating") +
scale_x_discrete(labels=rep(c("F","M"),4)) +
theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
      axis.title.x = element_blank(),
      plot.margin=unit(c(2,2,15,2), "mm")) +
stat_compare_means(method = "t.test",comparisons = my_comparisons,
                   label.y = 110, label = "p.signif") +
coord_cartesian(ylim=c(20,120), xlim=c(1,4), clip="off") +
annotate("text", x=1.5, y=0, label=c("","","Private","Private")) +
annotate("text", x=3.5, y=0, label=c("","","Public","Public")) +
annotate("text", x=0.5, y=10, label=c("","","Sex",""), hjust=1) +
annotate("text", x=0.5, y=0, label=c("","","School",""), hjust=1)
新增内容包括
scale\u x\u discrete()
更改x轴标签,
plot.margin
coord\u cartesian
允许在绘图区域之外进行注释,以及
annotate
为每个注释提供注释,其中每个镶嵌面面板的标签作为向量给出,不应获得标签的面板使用空格


可能有一种更干净的方法可以做到这一点,但绘图的面特性意味着注释可以跨面复制,而在这种情况下,您不需要这些面。

您的X轴标签应该是什么样子的?上面的绘图就是我的X轴的样子。您可以运行我的代码来获得我不喜欢的x轴标签的绘图