-2

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)
Community
  • 1
  • 1
Luo Lei
  • 649
  • 2
  • 9
  • 19
  • Your example is not reproducible, what is `portout`, also I think `ar_meds <-ddply(p,.(list(TYPE,PROVIDER)), summarise, med = median(AVGREV))` should be `ar_meds <-ddply(p,.(TYPE,PROVIDER), summarise, med = median(AVGREV))` – mnel Nov 14 '12 at 03:52

1 Answers1

10

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)

enter image description here

mnel
  • 113,303
  • 27
  • 265
  • 254