[R] ggplot geom_boxplot and stat_smooth

Petr PIKAL petr.pikal at precheza.cz
Tue Jan 25 08:05:59 CET 2011


Dear all

I would like to superpose some smoothing line through boxplot in ggplot

> dput(ad)
structure(list(konc.f = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 
7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 7L, 7L, 7L), .Label = 
c("(189,196]", 
"(196,202]", "(202,208]", "(208,215]", "(215,221]", "(221,227]", 
"(227,234]"), class = "factor"), bel = c(99.28, 99.29, 99.25, 
98.13, 99.51, 99.21, 99.09, 97.84, 98.97, 98.48, 98.64, 98.09, 
98.44, 98.19, 98.25, 97.54, 99.11, 98.23, 97.62, 97.01, 97.62, 
97.58, 97.42, 97.75, 97.16, 96.79, 96.82, 98.8, 99.02, 98.86, 
98.85, 99.25, 98.46, 98.68, 98.3, 98.67, 98.54, 98.39, 98.18, 
98.54, 99.13, 98.92, 98.29, 98.78, 98.58, 98.78, 98.9, 98.18, 
97.5, 98.63, 97.53, 96.55, 96.52, 96.23, 95.54, 95.54, 96.33, 
95.91, 95.41, 94.98, 94.69, 93.95, 94.05, 95.22, 94.6, 94.27, 
93.44, 95.15, 94.62, 93.86, 94.51, 94.83, 93.66, 92.95, 94.4, 
93.17, 92.77, 95.79, 95.03, 94.96, 95.94, 95.71, 95.19, 95.11, 
94.91, 94.42, 94.68, 94.91, 94.66, 94.05, 93.57, 93.43, 94.77, 
93.58, 93.84, 93.24, 94.45, 93.57, 93.46, 92.38, 92.39, 94.07
), typ = structure(c(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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("alrut", 
"nealrut", "stan", "vlakan"), class = "factor")), .Names = c("konc.f", 
"bel", "typ"), row.names = c(NA, -102L), class = "data.frame")
>

Here is what I did

p<-ggplot(ad, aes(x=konc.f, y=bel, colour=typ))
p+geom_boxplot()+geom_jitter(position=position_jitter(w=0.1))+stat_smooth()
geom_smooth: Only one unique x value each group.Maybe you want aes(group = 
1)?

I get nice picture with boxes but I expected to get something like 
smoothing line through box centres. 

Is it possible without some hack to stat_smooth code?

Regards
Petr



More information about the R-help mailing list