https://rua.ua.es/dspace/bitstream/10045/69767/1/Modulo_4_-_Graficos_avanzados_con_ggplot2.pdf
data(iris)
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
?iris
names(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
• data= es obligatoriamente un conjunto de datos o “data.frame” • aes() es la configuración de los ejes • y la capa de gráficos propiamente dicha
library(ggplot2)
p<-ggplot(data=iris, aes(Sepal.Length, Petal.Length))
p
p+geom_point()
p+geom_point()+geom_line()
p+geom_point()+geom_line()+ stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p+geom_point()+ stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
t<-ggplot(data=iris, aes(Sepal.Length, Petal.Length,color=Species))
t+ geom_point() + stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
t+ geom_point() + stat_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
k<-ggplot(data=iris, aes(Sepal.Length, Petal.Length,color=Species,shape=Species))
k + geom_point() + stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
k<-ggplot(data=iris, aes(Sepal.Length, Petal.Length,size=Species))
k + geom_point() + stat_smooth()
## Warning: Using size for a discrete variable is not advised.
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Depende del ancho del petalo
k<-ggplot(data=iris, aes(Sepal.Length, Petal.Length,size=Petal.Width))
k + geom_point() + stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Si queremos cambiar el tamaño, podemos usar “size=” dentro de “geom_point()” para que sea mas visible.
ggplot(data=iris, aes(Sepal.Length, Petal.Length,shape=Species)) +
geom_point(size=1) + # los puntos
stat_smooth(se=F) # líneas y bandas de suavizado (smooth)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(data=iris, aes(Sepal.Length, Petal.Length)) +
geom_point() + # los puntos
stat_smooth() + # líneas y bandas de suavizado (smooth)
facet_wrap(~ Species) # las especies van en gráficos distintos
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Boxplot o diagramas de cajas
# Boxplot o diagramas de cajas
ggplot(data=iris, aes(iris$Species, Petal.Length)) +
geom_boxplot() # dibujamos el diagrama de cajas
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
ggplot(data=iris, aes(iris$Species, Petal.Length)) +
geom_boxplot() + # dibujamos el diagrama de cajas
stat_summary(fun.y=mean, geom="point", shape=18, size=3, color="red")
## Warning: `fun.y` is deprecated. Use `fun` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
# Añadir los puntos observados, con un poco de desplazamiento (jitter).
ggplot(data=iris, aes(iris$Species, Petal.Length)) +
geom_boxplot() + geom_jitter()
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
ggplot(data=iris, aes(iris$Species, Petal.Length)) +
geom_boxplot(notch = TRUE) + # dibujamos el diagrama de cajas
stat_summary(fun.y=mean, geom="point", shape=18, size=3, color="red") #pintamos la media con un punto ROJO
## Warning: `fun.y` is deprecated. Use `fun` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
ggplot(data=iris, aes(iris$Species, Petal.Length)) +
geom_violin() + stat_summary(fun.y=mean, geom="point", shape=18, size=3, color="red")
## Warning: `fun.y` is deprecated. Use `fun` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
# Histogramas
ggplot(data=iris, aes(Petal.Length,fill=Species)) + geom_histogram() # dibujamos el histograma
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Graficos de densidad
ggplot(data=iris, aes(Petal.Length,fill=Species)) + geom_density(alpha=0.7) # dibujamos el diagrama de densidad
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p<-ggplot(data=iris, aes(Sepal.Length, Petal.Length))+geom_point()
ggplotly(p)
p<-p+geom_point()+geom_line()
ggplotly(p)
p<-ggplot(data=iris, aes(iris$Species, Petal.Length)) +
geom_boxplot() + geom_jitter()
ggplotly(p)
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.
## Warning: Use of `iris$Species` is discouraged. Use `Species` instead.