ggplot2

Instalación y carga

# Esto se hace una sola vez
## install.packages("ggplot2")
## install.packages("plotly")
## install.packages("dslabs")
## install.packages("dplyr")

library(dslabs)
library(ggplot2)
library(plotly) #Biblioteca de gráficos interactivos
## 
## 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
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data()

Componentes Básicos de ggplot2

Definir los Datos, Aesthetics y Geometría

## Dataset
## Cargar el conjunto de datos mtcars
data(mtcars)

# Crear un gráfico de dispersión básico
ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()

Personalización

  # Cambiar color y tamaño de los puntos
  # Etiqueta del eje x
  # Título del gráfico
  # Etiqueta del eje y

ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "blue", size = 3) +            
    labs(title = "Peso vs. Consumo de Combustible",  
       x = "Peso (1000 lbs)",                      
       y = "Millas por Galón")                 

Facetado

# Crear subgráficos por número de cilindros
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  facet_wrap(~ cyl)  

Temas

# Usar un tema minimalista

ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme_minimal()  

Ejercicio completo

# Color por número de cilindros
# Leyenda de colores
# Centrar el título

g <- ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point(aes(color = factor(cyl)), size = 3) +  
  labs(title = "Peso vs. Consumo de Combustible",
       x = "Peso (1000 lbs)",
       y = "Millas por Galón",
       color = "Cilindros") +                      
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))    

Gráfico interactivo

interactive_plot <- ggplotly(g)
interactive_plot

EJERCICIO SCRIPT PROFE

Cargar la BD ‘murders’

data("murders")

4 gráficos

## Esto se corre completo, las 3 líneas
ggplot(data = murders)

ggplot(data=murders,aes(x = population/10^6, y = total))+
  geom_point()

murders %>% ggplot(aes(x = population/10^6, y = total)) + 
  geom_point()

murders %>% ggplot() + 
  geom_point(aes(x = population/10^6, y = total))

murders %>% ggplot(aes(x = population/10^6, 
                       y = total,color=region,shape=region)) + 
  geom_point(show.legend = FALSE)+xlab("Populations in millions (log scale)") + 
  ylab("Total number of murders (log scale)") +
  ggtitle("US Gun Murders in 2010")+ 
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") + 
  facet_wrap(~ region, nrow = 2)

4 gráficos

murders %>% ggplot(aes(x = population/10^6, 
                       y = total,color=region,shape=region)) + 
  geom_point(show.legend = FALSE)+xlab("Populations in millions (log scale)") + 
  ylab("Total number of murders (log scale)") +
  ggtitle("US Gun Murders in 2010")+ 
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") + 
  facet_wrap(~ region, nrow = 2)+ 
  geom_smooth(show.legend = FALSE)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

p<- murders %>% ggplot(aes(x = population/10^6, 
                           y = total,color=region)) + 
  geom_point(aes(shape=region))+xlab("Populations in millions (log scale)") + 
  ylab("Total number of murders (log scale)") +
  ggtitle("US Gun Murders in 2010")+ 
  scale_x_continuous(trans = "log10") +
  scale_y_continuous(trans = "log10") 
p

Título

Título