改变盒形图的布局并添加标签

8
我想要(得到建议这样做)创建带有不同外观和标签的箱线图。期望的(不完整)输出看起来像下面这样(每个箱子都有四分位数标签和样本大小)。
 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose", names = supp )

  # some  unsuccessful trials 
 # to add names 
 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose", names = supp*dose)
 # to remove the plot outline 
 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose", bty="n")
1个回答

9

这应该能帮到你了。关键是要知道,如果你在设置plot = FALSE的同时将boxplot的结果保存到一个对象中,你就可以得到每个对象放置的所有信息。然后,您可以使用此信息通过text添加文本。

d <- boxplot(len~supp*dose, data=ToothGrowth,plot = FALSE)

 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose",axes = FALSE )

for (i in 1:ncol(d$stats)){
    text(i,d$stats[,i],labels = d$stats[,i],cex = 0.75,adj = c(0.5,0))
    text(i,d$stats[5,i]+1,labels = paste0("n=",d$n[i]),cex = 0.75)
    text(i-0.25,d$stats[3,i],labels = d$names[i],adj = 1,cex = 0.75)
}

然而,我要指出的是,无论是谁建议您:

  • 删除坐标轴
  • 在每个箱线图上注明分位数值和样本大小

都不应该向任何人提供制作图表的建议。他们成功地使您的箱线图变得更糟。

enter image description here


3
谢谢,我希望我能够传达你的信息 - 有时候我会收到这样的建议(命令!),我需要遵循! - shNIL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接