如何更改ggboxplot中的x轴标签 [英] how to change x-axis labels in ggboxplot

查看:1157
本文介绍了如何更改ggboxplot中的x轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个因素的数据框.我使用ggboxplot来获得带有不同类别比较的箱形图.我对x轴标签不满意.我尝试了不同的方法,但未能达到我的期望. 用于创建绘图的代码为:

I have a data frame including multiple factors. I used ggboxplot to get a box plot with comparisons for different categories. I am not satisfied with the x axis labels. I tried different ways but failed to get what I expected. The code used to create a plot is:

    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")

预期情节如下:

推荐答案

这使您接近要查找的内容(我不知道行分隔符).您可能还必须尝试调整标签的位置,以使它们和大小正确无误.

This gets you close to what you're looking for (I couldn't figure out the line separator). You may also have to play around with the positioning of the labels to get them just right, as well as sizes.

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_x_discrete()可以更改x轴标签,plot.margincoord_cartesian允许在绘图区域之外添加注释,以及annotate的每个注释,其中每个小平面面板的标签均以矢量形式给出,在面板上留有空白,这些空白不应该带有标签.

Additions include scale_x_discrete() to change x-axis labels, plot.margin and coord_cartesian to allow annotations outside the plot area, and annotate for each annotation, where the labels for each facet panel are given as a vector, with blanks for panels which shouldn't get labels.

也许有一种更干净的方法可以执行此操作,但是绘图的多面性质意味着注释会在您不希望出现的情况下跨多个面进行复制.

There may be a cleaner way to do this, but the faceted nature of the plot means that annotations get replicated across facets which you don't want in this case.

这篇关于如何更改ggboxplot中的x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆