Daniela Ballari, 2018
dballari@uazuay.edu.ec



Este es un archivo Notebook de R Markdown, Cuando ejecutas el código con Run o Ctrl+Shift+Enter, el resultado aparecerá debajo del código

Función ggplot() de ggplot

Este tutorial se basa en http://seananderson.ca/ggplot2-FISH554/ y https://rpubs.com/agrogan/ggplot2

Comienza cargando la librería

library(ggplot2)

Los elementos básicos de ggplot son

ggplot(): especificar los datos geom_point(): la geometría o tipo de gráfico puede ser de muchas maneras, en este caso es de tipo punto aes(): “aesthetic”, como las variables serán visualizadas facet_grid(): gráficos compartidos.

Los elementos de ggplot se unen con el símbolo +

ggplot(data = mtcars) + 
  geom_point(aes(mpg, qsec, colour = factor(am))) +
  facet_grid(~vs)

Algunos tipos de geometría son geom_point() Points geom_line() Lines geom_ribbon() Ribbons, y range with continuous x values geom_polygon() Polygon, a filled path geom_pointrange() Vertical line with a point in the middle geom_linerange() An interval represented by a vertical line geom_path() Connect observations in original order geom_histogram() Histograms geom_text() Text annotations geom_violin( Violin plot (another name for a beanplot) geom_map() Polygons on a map

Histogramas

ggplot(data = mtcars) + geom_histogram(aes(x=qsec))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

El siguiente es un código idéntico al anterior

ggplot(data = mtcars, aes(x = qsec)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Limita el número de clases

ggplot(data = mtcars) + geom_histogram(aes(x=qsec),bins=10)

Divide en categorias y asigna trasparencia

ggplot(data = mtcars) + geom_histogram(aes(x=qsec,fill=factor(am)),bins=10, position = "stack",alpha = 0.5)#stack

ggplot(data = mtcars) + geom_histogram(aes(x=qsec,fill=factor(am)),bins=10, position = "identity",alpha = 0.5)

Ancho de barras

ggplot(data = mtcars) + geom_histogram(aes(x=qsec,fill=factor(am)),bins=10, position = "stack",alpha = 0.5, binwidth=0.3)

Gráfico de densidad

ggplot(data = mtcars) + geom_density(aes(x=qsec,fill=factor(am)),bins=10, position = "identity",alpha = 0.5)
## Warning: Ignoring unknown parameters: bins

Boxplots

ggplot(data = mtcars) + geom_boxplot(aes(x=mpg, y=mpg))
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?

ggplot(data = mtcars) + geom_boxplot(aes(x=factor(am), y=mpg))

ggplot(data = mtcars) + geom_boxplot(aes(x=factor(am), y=mpg)) + coord_flip()

Aesthetics

Color, forma, tamaño y transparencia

ggplot(mtcars, aes(mpg, qsec)) + geom_point(colour = "red")

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(colour = factor(cyl)))

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(size = cyl), alpha = 0.4)

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(shape = factor(cyl)), alpha = 0.4)

#### Facet wrap y grid

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(size = hp), alpha = 0.4)+
  facet_wrap(~factor(am))

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(size = hp), alpha = 0.4)+
 facet_grid(factor(cyl)~factor(am))

Scale free

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(size = hp), alpha = 0.4)+
facet_grid(factor(cyl)~factor(am), scale="free")

Titulos y nombres de ejes

ggplot(mtcars, aes(mpg, qsec)) + geom_point(aes(size = hp), alpha = 0.4)+
 facet_grid(factor(cyl)~factor(am), scale="free")+
xlab("Nombre")+ # eje x
ylab("Nombre")+ # eje y
ggtitle("Título")+ #título del gráfico
labs(size = "Caballo de potencia") # título de legenda

Gráficos compuestos

# install.packages(gridExtra)
library(gridExtra)
df <- data.frame(x = 1:10, y = rnorm(100))
p1 <- ggplot(df, aes(x, y)) + geom_point()
p2 <- ggplot(df, aes(y)) + geom_histogram()
grid.arrange(p1, p2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

grid.arrange(p1, p2, ncol=2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Actividad individual 1

Utiliza los datos de WorldPhones y dibuja las series temporales con puntos y lineas de los diferentes continentes.

Actividad individual 2

Explora en google ejemplos de ggplot.

Actividad individual 3

Repite la actividad anterior pero esta vez con datos propios.