r - 更改箱线图的布局并为其添加标签

标签 r plot ggplot2 lattice boxplot

我想(得到建议)创建具有不同外观的箱线图并为其添加标签。预期(不完整)输出将如下所示(每个框都有四分位标签)和样本大小。

enter image description here

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

最佳答案

这应该让你开始。关键是要知道如果保存boxplot的结果设置时进入对象 plot = FALSE ,您可以获得每个对象去向的所有信息。然后您可以使用此信息通过 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

    关于r - 更改箱线图的布局并为其添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977561/

    相关文章:

    r - 图例位置,ggplot,相对于绘图区域

    r - R包:当我的导出函数未从其他包中显式调用函数,而子例程执行时,“import”的工作方式

    r - 如何在 R 中制作可变宽度直方图,标签与 bin 边缘对齐?

    r - 在堆积的条形图中绘制不同元素之间的线

    python - 解决 n 皇后难题

    emacs - 开发 Sweave 文档的建议

    matlab - 在 matlab 中绘制一堆 3d 线的最有效方法

    python - 防止绘制 pandas df 列的 1 个级别/值

    R Plotly - 绘制多条回归线

    r - ggplot, geom_tile : plot continuous variable using irregular, 用户定义的离散间隔