Trabajamos con tres variables continuas y dos factores:
vx <- c( rnorm(50), rnorm(50,.75,.2) )
vy <- rnorm(100, 1,.1)
vz <- rnorm(100, 2 ,.1)
sex <- c( rep("M", 50),rep("H", 50) )
orig <- c( rep("Murcia", 25), rep("Hamburg", 50), rep("New York", 25) )
data <- data.frame( vx, vy, vz, sex, orig, stringsAsFactors = TRUE )
head( data )
## vx vy vz sex orig
## 1 -0.62045769 0.9653264 2.070401 M Murcia
## 2 -0.58137768 0.9320061 2.089330 M Murcia
## 3 0.07497102 1.1912565 1.956361 M Murcia
## 4 -1.73098715 0.9804755 2.206994 M Murcia
## 5 0.44492290 0.9684064 2.218842 M Murcia
## 6 0.21992985 0.8638582 2.118747 M Murcia
La primera sentencia me determina los orígenes de datos y los aesthetic básicos: voy a poner a la variable orig en el eje OX y a representar la variable xv, el color será la variable sex en algunos casos.
library(ggplot2)
q <- ggplot(data, aes(x=orig, y=vx) )
ahí van:
q + geom_boxplot() +
geom_point( )
q + geom_boxplot() +
geom_point(aes(color=sex))
q + geom_violin() +
geom_point()
q + geom_violin() +
geom_point(aes(color=sex))
# q + geom_dotplot() # más feo q picio
q + geom_jitter(width = 0.1, aes(color=sex))
Generalmente queremos meter más variables a la vez con el fin de comparar o ahorrar espacio o plots para el articulo. Si quiero representar más de una variable continua a la vez debo de “remodelar” el dataframe.
Usamos la librería reshape y la función melt(), creada por la misma persona que desarrollo ggplot2: Hadley Wickham (tres hurras!).
library(reshape)
data2 <- melt(data, id=c("sex", "orig"))
head(data2)
## sex orig variable value
## 1 M Murcia vx -0.62045769
## 2 M Murcia vx -0.58137768
## 3 M Murcia vx 0.07497102
## 4 M Murcia vx -1.73098715
## 5 M Murcia vx 0.44492290
## 6 M Murcia vx 0.21992985
Aquí está el quid de la cuestión debo proporcionar a la función melt() un dataframe refinado, en id las variables q identifiquen los grupos, y quitar las variables q no me valgan…
Vuelvo a crear un objeto ggplot básico:
q2 <- ggplot(data2, aes(x=variable, y=value))
y a pintar:
q2 + geom_violin() +
geom_point(aes(color=sex, shape=orig), alpha = 1/5)
q2 + geom_jitter(width = 0.2, aes(color=sex, shape=orig))
sessionInfo()
## R version 3.4.3 (2017-11-30)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.3 LTS
##
## Matrix products: default
## BLAS: /usr/lib/libblas/libblas.so.3.6.0
## LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
##
## locale:
## [1] LC_CTYPE=es_ES.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=es_ES.UTF-8 LC_COLLATE=es_ES.UTF-8
## [5] LC_MONETARY=es_ES.UTF-8 LC_MESSAGES=es_ES.UTF-8
## [7] LC_PAPER=es_ES.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] reshape_0.8.7 ggplot2_2.2.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.17 digest_0.6.12 rprojroot_1.2 plyr_1.8.4
## [5] grid_3.4.3 gtable_0.2.0 backports_1.1.0 magrittr_1.5
## [9] evaluate_0.10.1 scales_0.5.0 pillar_1.2.3 rlang_0.2.1
## [13] stringi_1.2.3 lazyeval_0.2.0 rmarkdown_1.6 labeling_0.3
## [17] tools_3.4.3 stringr_1.3.1 munsell_0.4.3 yaml_2.1.14
## [21] compiler_3.4.3 colorspace_1.3-2 htmltools_0.3.6 knitr_1.17
## [25] tibble_1.4.2
H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2009.
H. Wickham. Reshaping data with the reshape package. Journal of Statistical Software, 21(12), 2007.