Tonja Tonja - 14 days ago 5
R Question

ggplot: fill the box plots with the color

I have data with the rates from which I do box plots grouped by version. I want to color each box by a device. I also have another data2 that contains the numbers I want to display for each

version
. However, it seems that my geom_box has conflicts with it.
It shows me an error:
Error in eval(expr, envir, enclos) : Object 'device' not found

p <- ggplot(data1, aes(factor(version),rates,fill=device))
p + geom_boxplot() + geom_text(data = data2, aes(x = version, y = 0, label = count))


data1:

rates version device
1 0.56 Windows/desktop/Chrome desktop
2 0.13 iOS/mobile/Safari mobile mobile
3 0.15 iOS/tablet/Safari mobile tablet
4 0.65 Windows/desktop/Firefox desktop
5 0.17 OS X/desktop/Safari desktop
6 0.83 Windows/desktop/Chrome desktop
7 0.39 Windows/desktop/Firefox desktop
8 0.63 Windows/desktop/Chrome desktop
9 0.61 Windows/desktop/Internet Explorer desktop
10 0.00 Windows/desktop/Chrome desktop


data2:

version count
21188 iOS/mobile/Safari mobile 37545
105488 Windows/desktop/Firefox 28030
82478 Windows/desktop/Chrome 23936

Answer

This should work:

ggplot(data1, aes(factor(version),rates)) +
  geom_boxplot(aes(fill=device) + 
  geom_text(data = data2, aes(x = version, y = 0, label = count))

It is trying to find the fill variable in data2, but it isn't there.