如何显示ggplot中多面箱线图的中值? [英] How to display the median value in a faceted boxplot in ggplot?

查看:425
本文介绍了如何显示ggplot中多面箱线图的中值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后续跟我以前的问题关于如何在boxplot中显示价值标签
现在我喜欢在多面箱型图中显示价值标签。我有点卡住了。
问题更多的是通过geom_text()来识别二维数组或数据框。

  p < - 结构(列表(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, ,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,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)。标签= c(其他,
Skinny Mobile (3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,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, 2L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L, 3L,
3L,3L ,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L, ,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,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.44416666666667,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.0555,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,aes(x = TYPE,y = TOTALREV))+ geom_boxplot()计算p(.list(TYPE,PROVIDER))的总和,med = median(AVGREV))
ar.m < - ggplot + facet_wrap(〜TYPE)
ar.m + geom_text(data = ar_meds,aes(x = TYPE,y = med,label = round(med)),size = 3,vjust = -0.5)


解决方案

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



如果您希望绘图中的每个boxplot的中值,则需要确保data.frame(或调用环境)中存在的faceting变量用于 geom _....



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

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

#按类型和提供者获取中位数(两列
#将(DF,。(TYPE,PROVIDER),summary,med = median(VALUE))

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


Follow up with my previous quesiton on how to display value labels in boxplot Now I like to display value labels in faceted boxplots. I am a bit stuck again. Problem is more of getting geom_text() to recognise the two dimensional array or dataframe.

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)

解决方案

Your question is full of syntax errors and references to variables and objects that don't exist.

If you want median values for each boxplot when the plots are faceted, then you need to ensure that the faceting variables exist in the data.frame (or calling environment) used each call to geom_....

A simple, reproducible example

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)

这篇关于如何显示ggplot中多面箱线图的中值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆