Libreria Grafica de R usando ggplot2

A continuacion se mostrarán diferentes graficos con R usando la libreria ggplot2.

Grafico basico de ggplot2 (grafico de dispersión)

# library: cargar la libreria
library(ggplot2)
 
# The iris dataset is provided natively by R
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
# basic scatterplot
#<function>(data_set, elementos_Esteticos(eje_x = , eje_y = ))+
#geometria
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
    geom_point()

Grafico con agrupamientos usando ggplot2 (grafico de dispersión)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
  geom_point()

Grafico con agrupamientos usando ggplot2 (grafico de dispersión) y también diferentes figuras

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species)) +
  geom_point()

Quiero saber cuales son las flores con el sepalo mas grande.

Grafico con agrupamientos usando ggplot2 (grafico de dispersión), diferentes figuras, diferentes tamanos del sepalo

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
  geom_point()

Imprimiendo Facets

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
  geom_point() +
  facet_wrap(~Species)

Cambiando el tema

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
  geom_point() +
  facet_wrap(~Species) +
  theme_bw()

Adiciónando una linea de tendencia no lineal

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
  geom_point() +
  facet_wrap(~Species) +
  geom_smooth(color = "red", se = FALSE) +
  theme_bw()

Adiciónando una linea de tendencia lineal

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
  geom_point() +
  facet_wrap(~Species) +
  geom_smooth(color = "red", se = FALSE, method = "lm") +
  theme_bw()

Cambiando nombre de los ejes, titulo, y un caption.

grafico_completo <- 
  ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Sepal.Width, shape = Species)) +
  geom_point() +
  facet_wrap(~Species) +
  geom_smooth(color = "red", se = FALSE) +
  labs(x = "Longitud del sepalo (cm)", y = "Ancho del sepalo (cm)", 
       title = "Ancho vs largo de sepalos de flores", 
       caption = "Est. UCEVA 9no semestre") +
  theme_bw()

grafico_completo

Guardar grafico en mi carpeta usando ggsave

ggsave("imagenes/grafico_completo.png", plot = grafico_completo, units = "in", dpi = 300, height = 6, width = 9)

Haciendo el grafico interactivo

library(plotly)
# convirtiendo el grafico en interactivo
ggplotly(grafico_completo)

Graficando con un boxplot

ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
  geom_boxplot()

Graficando con un boxplot + puntos de datos usando geom_jitter angosto

cajas <- 
  ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
  geom_boxplot() +
  geom_jitter(width = 0.1) +
  labs(title = "Cajas de longitud petalo de Iris", y = "Longitud de petalo (cm)", 
       x = NULL, caption = "Estudiantes de 9no semestre - UCEVA") +
  theme_dark()

convirtiendo a la caja en interactivo

ggplotly(cajas)

Guardar el grafico de cajas

ggsave("imagenes/cajas.png", plot = cajas, units = "in", dpi = 300, height = 6, width = 9)