Unexpected beahviour with facet_grid

20 views
Skip to first unread message

Alain Guillet

unread,
Aug 4, 2016, 8:29:53 AM8/4/16
to ggplot2, alain....@uclouvain.be
Hello,

I use ggplot2 in order to represent the same data during 3 periods so I call facet_grid to get one subgraph by period. But when I do so, I get different results between the call on the whole data and the one on only one period (I expect to get one of the subgraphs to be identical to the graph obtained when using only one period).

I added the code and my session info hereunder. Could you explain me what I do worng or if there is a bug? Thank you.

Kind regards,
Alain

------------------------------

library(ggplot2)

# data
tmp <- data.frame(x=rnorm(9000),y=rnorm(9000),color=factor(rep(1:3,each=3000)),period=factor(rep(1:3,3000)),ligne=factor(rep(1:2,4500)))

# plot with the three periods
ggplot(tmp,aes(x=x,y=y,col=color,linetype=ligne))+geom_smooth()+scale_colour_manual(values=c("black","blue","yellow"))+guides(linetype=FALSE,col=FALSE)+facet_grid(period~.)

#plot with only the first period
ggplot(tmp[tmp$period=="1",],aes(x=x,y=y,col=color,linetype=ligne))+geom_smooth()+scale_colour_manual(values=c("black","blue","yellow"))+guides(linetype=FALSE,col=FALSE)+facet_grid(period~.)

------------------------------

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C LC_TIME=en_GB.UTF-8
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

other attached packages:
[1] doBy_4.5-15   ggplot2_2.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      lattice_0.20-33  digest_0.6.9 MASS_7.3-45      grid_3.3.1
 [6] plyr_1.8.4       nlme_3.1-128     gtable_0.2.0 magrittr_1.5     scales_0.4.0
[11] stringi_1.1.1    reshape2_1.4.1   Matrix_1.2-6 labeling_0.3     tools_3.3.1
[16] stringr_1.0.0    munsell_0.4.3    colorspace_1.2-6 mgcv_1.8-12

Sam Albers

unread,
Aug 4, 2016, 12:44:08 PM8/4/16
to Alain Guillet, ggplot2, alain....@uclouvain.be
Hi Alain,

I think your issue lies with geom_smooth(). From the help page:

"smoothing method (function) to use, eg. lm, glm, gam, loess, rlm. For
datasets with n < 1000 default is loess. For datasets with 1000 or
more observations defaults to gam, see gam for more details."

I was going to say that the subset of tmp to only period 1 was less
than 1000. However it is 3000:

nrow(tmp[tmp$period=="1",])

So I am a bit confused there. Nevertheless, ggplot2 does appear to
choosing a different smoothing method for the two calls. If you set it
manually with say LOESS they then appear identical. So if you really
want to forge ahead, simply choosing a smoothing method and use that
here. If you want to understand exactly what is going on, wait for
other more knowledgeable wizaRds to chime in here. For the example
below I've made use of grid.arrange to facilitate comparison:

# plot with the three periods
AllLevelsPlt <- ggplot(tmp,aes(x=x,y=y,col=color,linetype=ligne))+
geom_smooth(method="loess", se=FALSE)+
scale_colour_manual(values=c("black","blue","yellow"))+
guides(linetype=FALSE,col=FALSE)+
facet_grid(period~.)

#plot with only the first period
OnlyPeriod1Plt <-
ggplot(tmp[tmp$period=="1",],aes(x=x,y=y,col=color,linetype=ligne))+
geom_smooth(method="loess", se=FALSE)+
scale_colour_manual(values=c("black","blue","yellow"))+
guides(linetype=FALSE,col=FALSE)+
facet_grid(period~.)

## Combine into one plot for comparison
grid.arrange(OnlyPeriod1Plt, AllLevelsPlt, heights=c(1,3))

HTH,

Sam
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages