gpt4 book ai didi

r - 如何更改ggboxplot中的x轴标签

转载 作者:行者123 更新时间:2023-12-02 20:07:26 26 4
gpt4 key购买 nike

我有一个包含多个因素的数据框。我使用 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")

预期的情节如下所示: enter image description here

最佳答案

这让您更接近您要查找的内容(我不知道行分隔符)。您可能还需要调整标签的位置,使它们和尺寸恰到好处。

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 为每个注释,其中每个方面面板的标签以矢量形式给出,不应该获得标签的面板为空白。

可能有更简洁的方法来做到这一点,但情节的多面性意味着注释会跨多面复制,这在这种情况下是您不希望的。

关于r - 如何更改ggboxplot中的x轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54354718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com