[R] ggplot2 - unexpected beahviour with facet_grid
Alain Guillet
alain.guillet at uclouvain.be
Thu Aug 4 11:08:23 CEST 2016
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
--
Alain Guillet
Statistician and Computer Scientist
SMCS - IMMAQ - Université catholique de Louvain
http://www.uclouvain.be/smcs
Bureau c.316
Voie du Roman Pays, 20 (bte L1.04.01)
B-1348 Louvain-la-Neuve
Belgium
Tel: +32 10 47 30 50
Accès: http://www.uclouvain.be/323631.html
More information about the R-help
mailing list