[英]how to change x-axis labels in ggboxplot
我有一個包含多個因素的數據框。 我使用 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")
這讓你接近你正在尋找的東西(我無法弄清楚行分隔符)。 您可能還需要調整標簽的位置以使其恰到好處,以及尺寸。
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.margin
和coord_cartesian
繪圖區外以允許注釋和annotate
為每個注釋,其中對於每個小面面板標簽給出作為載體,與毛坯板,其不應該沒有標簽。
可能有一種更簡潔的方法來做到這一點,但情節的分面性質意味着注釋會在您不想要的分面之間復制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.