【问题标题】:How to display the median value in a faceted boxplot in ggplot?如何在ggplot的多面箱线图中显示中值?
【发布时间】:2012-11-14 03:47:49
【问题描述】:

跟进我之前在how to display value labels in boxplot 上的问题 现在我喜欢在多面箱线图中显示值标签。我又有点卡住了。 问题更多的是让 geom_text() 识别二维数组或数据框。

p <- structure(list(PROVIDER = structure(c(4L, 3L, 4L, 4L, 3L, 4L, 
3L, 4L, 4L, 3L, 4L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 3L, 4L, 1L, 1L, 1L, 1L, 
1L, 4L, 1L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 4L, 3L, 3L, 4L, 4L, 4L, 3L, 2L, 2L, 3L, 3L, 
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 3L, 4L, 1L, 3L, 3L), .Label = c("Other", 
"Skinny Mobile", "TNZMobile", "Vodafone NZ Mobile"), class = "factor"), 
    TYPE = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 
    3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 2L, 3L, 3L, 3L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L
    ), .Label = c("PM BUSINESS", "PM CONSUMER", "PREPAY"), class = "factor"), 
    AVGREV = c(10.426, 4.4175, 33.0408333333333, 49.71, 0, 6.778, 
    15.2858333333333, 0, 14.5558333333333, 13.3955555555556, 
    51.6075, 14.25, 11.85, 6.42833333333333, 4.1725, 14.7383333333333, 
    12.97375, 66.65, 11.4866666666667, 15.6533333333333, 16.7541666666667, 
    2.38428571428571, 77.475, 16.3316666666667, 3.414, 13.2466666666667, 
    13.905, 0.740833333333333, 36.2158333333333, 6.91, 16.4141666666667, 
    65.3116666666667, 18.0225, 127.831666666667, 15.2888888888889, 
    4.35166666666667, 7.38416666666667, 13.904, 4.34833333333333, 
    30.685, 11.6575, 42.844, 13.6216666666667, 24.6241666666667, 
    26.6441666666667, 93.391, 66.3966666666667, 9.89333333333333, 
    0, 34.998, 12.8836363636364, 7.44571428571429, 10.4772727272727, 
    35.5233333333333, 11.7911111111111, 12.82, 0.161111111111111, 
    0.36, 14.0775, 4.96571428571429, 20.7675, 8.4375, 7.3075, 
    1.71833333333333, 0.0725, 1.44916666666667, 0, 2.875, 10.9466666666667, 
    0, 11.8255555555556, 25.745, 0, 0, 21.3858333333333, 38.9108333333333, 
    6.32142857142857, 28.2625, 5.95166666666667, 22.5675, 12.1541666666667, 
    55.8766666666667, 15.5883333333333, 14.2175, 12.7991666666667, 
    1.8075, 13.845, 8.08333333333333, 8.7, 43.0991666666667, 
    19.2316666666667, 7.055, 7.47571428571429, 33.2175, 14.1625, 
    0, 5.204, 0, 77.39, 77.39)), .Names = c("PROVIDER", "TYPE", 
"AVGREV"), row.names = 23961:24060, class = "data.frame")

ar_meds <-ddply(p,.(list(TYPE,PROVIDER)), summarise, med = median(AVGREV))
ar.m <- ggplot(p,aes(x=TYPE, y=TOTALREV))+geom_boxplot() +facet_wrap(~TYPE)
ar.m+geom_text(data = ar_meds, aes(x = TYPE, y = med, label = round(med)),size = 3, vjust = -0.5)

【问题讨论】:

  • 你的例子不可复现,portout是什么,我也觉得ar_meds &lt;-ddply(p,.(list(TYPE,PROVIDER)), summarise, med = median(AVGREV))应该是ar_meds &lt;-ddply(p,.(TYPE,PROVIDER), summarise, med = median(AVGREV))

标签: r ggplot2


【解决方案1】:

您的问题充满了语法错误以及对不存在的变量和对象的引用。

如果你想要在图被分面时每个箱线图的中值,那么你需要确保分面变量存在于每次调用geom_....时使用的data.frame(或调用环境)中

一个简单的、可重现的例子

set.seed(1)
library(plyr)
DF <- data.frame(TYPE = sample(letters[1:3], 500, replace = TRUE), 
  PROVIDER = letters[1:5], VALUE = rnorm(500))

# get the medians by type and provider (both columns
# will exist in the new data.frame

meds <- ddply(DF, .(TYPE, PROVIDER), summarize, med = median(VALUE))

ggplot(DF, aes(x=PROVIDER,y=  VALUE)) + 
   geom_boxplot() + facet_wrap(~TYPE) + 
   geom_text(data = meds, aes(y = med, label = round(med,2)),size = 3, vjust = -0.5)

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 2012-08-31
    • 2019-04-02
    • 2021-10-30
    • 2014-08-23
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    相关资源
    最近更新 更多