Está en la página 1de 8

Ingeniería Biomédica UAM: CDB

R para ciencia de datos: Visualización.

R PARA CIENCIA DE DATOS : VISUAL IZACIÓN

EL DATAFRAME MILLAS
Observaciones para 38 modelos de automóviles

Head primeras filas y tape ultimas filas

C REANDO UN GRÁFICO CON GGPLOT

ggplot(data = dataframe)
+
tipo_grafico(mapping = aes(x = variable_eje_x , y = variable_eje_y))

ggplot dataframe sobre el que se va a representar una gráfica.


mapping cómo se mapean o se asignan las variables del conjunto de datos a propiedades visuales.
aes la estética o aesthetic del gráfico (variables y atributos).
geom_point tipo de grafica de scatterplot = diagrama de dispersión.
M APPINGS ESTÉTICOS : COLOR

(A) LOCAL: DENTRO DE AES

ggplot(data = dataframe)
+
tipo_grafico(mapping = aes(x = variable_eje_x , y = variable_eje_y , color =
variable_data))

(B) GLOBAL: FUERA DE AES

ggplot(data = nombre_data)
+
tipo_grafico(mapping = aes(x = variable_eje_x , y = variable_eje_y) , color =
variable_data)

(A) (B)
M APPINGS ESTÉTICOS : TAMAÑO

ggplot(data = nombre_data)
+
tipo_grafico(mapping = aes(x = variable_eje_x , y = variable_eje_y , size =
variable_data))

M APPINGS ESTÉTICOS : FORMA ( MÁXIMO 6)

ggplot(data = nombre_data)
+
tipo_grafico(mapping = aes(x = variable_eje_x , y = variable_eje_y , shape =
variable_data))
S EPARAR EN FACETAS

Las facetas son sub-gráficos que para cada tipo dentro de una misma variable. En el primer argumento
de facet_wrap hay que anteponer siempre el símbolo ~.

ggplot(data = nombre_data)
+
tipo_grafico(mapping = aes(x = variable_eje_x , y = variable_eje_y))
+
facet_wrap(~ variable_data , nrow = numero_filas_graficas)

M ÁS TIPOS DE GRÁFICOS

Diagrama de dispersión geom_point

Diagrama de barras geom_bar

Diagrama de líneas geom_line

Diagrama de caja geom_boxplot

Diagrama de línea suavizada geom_smooth

Histograma geom_histogram
EJEMPLO1: LINEALES

EJEMPLO2: HISTOGRAMA

Por defecto, el número de intervalos es de 30. Es posible establecer el número de intervalos (bins) y la
amplitud del intervalo (binwidth). Solo se pone una de ellas, la otra lo calculo automáticamente, ambas
no se pueden predeterminar.

pl <- ggplot(data = millas , aes(x = autopista))

pl + geom_histogram()

pl <- ggplot(data = millas , aes(x = autopista))

pl + geom_histogram(binwidth = 0.4 , col='black' , fill='green')

pl <- ggplot(data = millas , aes(x = autopista))

pl + geom_histogram(bins = 20 , col='black' , fill='blue')

EJEMPLO3: HISTOGRAMA

ggplot(data = millas , aes(x = autopista))


+
geom_histogram(bins = 0.4 , col='black' , fill='green')
+
labs(title = "Distribución de la eficiencia" , x = "Eficiencia" , y = "Número de coches")
E STÉTICA GROUP : VARIABLES CATEGÓRICAS

Un objeto distinto por cada valor de la variable. La diferencia con la anterior es que no aparece leyenda
y todas las líneas son iguales.

POR DEFECTO APARECE LEYENDA

ggplot(data = millas , aes(x = autopista))


+
geom_smooth(mapping = aes(x = cilindrada , y = autopista , color = traccion))

PARA QUITAR LEYENDA

ggplot(data = millas , aes(x = autopista))


+
geom_smooth(mapping = aes(x = cilindrada , y = autopista , color = traccion) ,
show.legend = FALSE)
M ÚLTIPLES GRÁFICOS O CAPAS

(A) FORMA LARGA

ggplot(data = millas)
+
geom_point(mapping = aes(x = cilindrada , y = autopista))
+
geom_smooth(mapping = aes(x = cilindrada , y = autopista))

(A) FORMA ABREVIADA

ggplot(data = millas , mapping = aes(x = cilindrada , y = autopista))


+
geom_point()
+
geom_smooth()

M APPINGS LOCALES

Si colocas mappings en un gráfico, ggplot2 los tratará como mappings locales para la capa.
Estas asignaciones serán usadas para extender o sobrescribir los mappings globales solo para esa capa.
Esto permite mostrar diferentes estéticas en diferentes capas.

ggplot(data = millas , mapping = aes(x = cilindrada , y = autopista))


+
geom_point(mapping = aes(color = clase))
+
geom_smooth()
D ISTINTOS CONJUNTOS DE DATOS EN CADA GRÁFICO

ggplot(data = millas , mapping = aes(x = cilindrada , y = autopista))


+
geom_point(mapping = aes(color = clase))
+
geom_smooth(data = filter(millas , clase == "subcompacto"))

También podría gustarte