【问题标题】:Add sample total num at x-axis for each box at boxplot in ggplot2在 ggplot2 的箱线图中为每个框在 x 轴上添加样本总数
【发布时间】:2013-01-27 21:20:10
【问题描述】:

我可以用经典的boxplot 做到这一点。这里我们使用内置数据:PlantGrown 为例。

attach(PlantGrowth)    
boxplot(weight~group,data=PlantGrowth,xaxt="n")
PlantGrowthSum=ddply(PlantGrowth,.(group),summarise,sum=length(weight))

> PlantGrowthSum
   group sum
1  ctrl  10
2  trt1  10
3  trt2  10

axis(1,1:3,paste(PlantGrowthSum$group,"(",PlantGrowthSum$sum,")",sep=""))

这里有个问题,ggplot2 怎么样?

library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group))
    + geom_boxplot()
    +theme(axis.text.x=element_blank())
    +theme(axis.text.x=1:3)
bp

但它失败了。关于应该设置哪个参数的任何线索?

【问题讨论】:

    标签: r ggplot2 boxplot


    【解决方案1】:

    由于在这种情况下 x 值是离散的,您应该使用 scale_x_discrete() 为 x 轴设置标签。

    bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group))+
    geom_boxplot()
    bp+scale_x_discrete(labels=paste(PlantGrowthSum$group,"(",PlantGrowthSum$sum,")",sep=""))
    

    关于 ggplot2 绘图的比例和其他元素的更多信息和示例可以在 ggplot2 文档site 中找到。

    【讨论】:

    • 胡须的大小计算方式不同吗?
    • @gd047 是的,它们的计算方式不同 - 请参阅ggplot2 mail list上的讨论
    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2021-11-23
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多