Unexpected behaviour in geom_boxplot.

23 views
Skip to first unread message

Sam Albers

unread,
Jan 29, 2016, 3:20:59 PM1/29/16
to ggplot2
Hello all,

I am running into some unexpected behaviour with geom_boxplot and I
can't seem to work on what I am doing wrong. I am expecting a boxplot
like this:

airquality %>%
filter(Day<10) %>%
ggplot(aes(x=Month, y=Wind, group=factor(Day), colour=factor(Day))) +
geom_boxplot()

Where there is a box for each value of the x-axis (airquality$Month)
in this case. However, when using my own dataset which has 4 values
for the x-axis (df$Temp1), essentially the same code produces only two
boxplots with no clear indication why. To properly reproduce this I
need to paste a dput() output. That is below. But I did want to ask
can anyone explain why I am experiencing this behaviour? I've aligned
the column class to see if that was the issue but that didn't seem to
fix the problem.

Any help here would be much appreciated.

##Code
library(dplyr)
library(ggplot2)

df <- structure(list(Year = c(1985, 1985, 1986, 1985, 1986, 1987, 1985,
1986, 1987, 1986, 1987, 1988, 1989, 1987, 1988, 1989, 1990, 1988,
1989, 1990, 1991, 1989, 1990, 1991, 1992, 1990, 1991, 1992, 1993,
1991, 1992, 1993, 1994, 1992, 1993, 1994, 1995, 1993, 1994, 1995,
1996, 1994, 1995, 1996, 1997, 1995, 1996, 1997, 1998, 1996, 1997,
1998, 1997, 1998, 1999, 1998, 1999, 2000, 1999, 2000, 2001, 2002,
2000, 2001, 2002, 2001, 2002, 2003, 2004, 2002, 2003, 2004, 2005,
2003, 2004, 2005, 2004, 2005, 2006, 2005, 2006, 2006, 2008, 2008,
2009, 2010, 2008, 2009, 2010, 2009, 2010, 2011, 2012, 2010, 2011,
2012, 2013, 2011, 2012, 2013, 2012, 2013, 2013, 2014, 2014),
Temp1 = structure(c(4L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 3L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 3L, 2L, 3L,
4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
1L, 2L, 1L, 2L, 1L), .Label = c("0", "1", "2", "3", "4",
"NA"), class = "factor"), value = c(184.444444444444, 171.107692307692,
187, 131.567279069767, 178.853658536585, 182, 53.7027521367521,
119.854538461538, 169.795918367347, 54.3664172043011, 124.090084337349,
173.279069767442, 186.4112, 54.36984375, 137.196428571429,
174.937780487805, 198.5, 58.3459292035398, 130.337333333333,
188.285714285714, 217.5, 54.8464581005587, 154, 213.7, 248,
56.9878761904762, 134.20125, 210.281797701149, 211.492344827586,
57.3128341968912, 147.112672272727, 188.657383157895, 227,
58.5859338988095, 147.960177719298, 212.481481481481, 230,
56.1980017710583, 137.533122571429, 188.926686746988, 185.791,
59.047311421875, 145.814175916473, 179.733600508906, 191.8946,
60.3286382535576, 132.075451148036, 176.581374796748, 206.090909090909,
55.256875539972, 141.198857185185, 185.884888392857, 58.70678125,
148.949788181818, 192.289764705882, 53.5368699547511, 117.309491428571,
214.277, 55.6431515686275, 120.428416, 202.988, 197.39205,
56.5451146938776, 125.954193548387, 187.30371, 56.4023109606299,
140.637079649123, 187.394, 189.087142857143, 54.1258163783784,
137.020974545455, 183.104521346154, 213.4284125, 57.3527583715013,
119.311938695652, 195.839588666667, 53.0352874821173, 118.8263952,
221.623948, 53.3582971782178, 129.468106666667, 59.4131793406593,
186.382048, 134.924196744186, 186.231634117647, 198.549956666667,
54.815382352438, 130.056582550336, 173.504794, 52.3947408376068,
136.033169315069, 173.867970178571, 182.476467692308, 51.5861292358079,
134.990225, 177.178461666667, 264.75057, 52.0909991555556,
126.604977368421, 248.57794, 57.8015161576355, 111.975898421053,
59.4611988067445, 109.443398571429, 58.2321326779661), var1 = c("Level1",
"Level1", "Level1", "Level1", "Level1", "Level1", "Level1",
"Level1", "Level1", "Level1", "Level1", "Level1", "Level1",
"Level1", "Level1", "Level1", "Level1", "Level1", "Level1",
"Level1", "Level1", "Level1", "Level1", "Level1", "Level2",
"Level1", "Level1", "Level2", "Level2", "Level1", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2", "Level2", "Level2", "Level2", "Level2",
"Level2", "Level2")), .Names = c("Year", "Temp1", "value",
"var1"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-105L))

df %>%
mutate(var1=factor(var1), Temp1=as.numeric(Temp1)) %>%
ggplot(aes(x=Temp1, y=value, group=var1, colour=var1)) +
geom_boxplot()


airquality %>%
filter(Day<10) %>%
ggplot(aes(x=Month, y=Wind, group=factor(Day), colour=factor(Day))) +
geom_boxplot()


Thanks in advance!

Sam

Brandon Hurr

unread,
Jan 29, 2016, 3:34:16 PM1/29/16
to Sam Albers, ggplot2

On Fri, Jan 29, 2016 at 12:20 PM, Sam Albers <tonights...@gmail.com> wrote:
df %>%
  mutate(var1=factor(var1), Temp1=as.numeric(Temp1)) %>%
  ggplot(aes(x=Temp1, y=value, group=var1, colour=var1)) +
  geom_boxplot()

Sam, 

I'm not entirely sure what's going on other than perhaps your grouping variable needs to be a bit "fancier". 

I just did this:

df %>%
  mutate(var1=factor(var1), Temp1=as.numeric(Temp1)) %>%
  ggplot(aes(x=Temp1, y=value, group=interaction(Temp1, var1), colour=var1)) +
  geom_boxplot()

and got:
Inline image 1

Is that what you're looking for? 

B

Sam Albers

unread,
Jan 29, 2016, 3:35:51 PM1/29/16
to Brandon Hurr, ggplot2
Works for me! Thanks!
Reply all
Reply to author
Forward
0 new messages