Práctica con histogramas
library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
library(ggplot2movies)
Observamos nuestro dataset, es un archivo que contiene 58788 películas y 24 variables.
str(movies)
## Classes 'tbl_df', 'tbl' and 'data.frame': 58788 obs. of 24 variables:
## $ title : chr "$" "$1000 a Touchdown" "$21 a Day Once a Month" "$40,000" ...
## $ year : int 1971 1939 1941 1996 1975 2000 2002 2002 1987 1917 ...
## $ length : int 121 71 7 70 71 91 93 25 97 61 ...
## $ budget : int NA NA NA NA NA NA NA NA NA NA ...
## $ rating : num 6.4 6 8.2 8.2 3.4 4.3 5.3 6.7 6.6 6 ...
## $ votes : int 348 20 5 6 17 45 200 24 18 51 ...
## $ r1 : num 4.5 0 0 14.5 24.5 4.5 4.5 4.5 4.5 4.5 ...
## $ r2 : num 4.5 14.5 0 0 4.5 4.5 0 4.5 4.5 0 ...
## $ r3 : num 4.5 4.5 0 0 0 4.5 4.5 4.5 4.5 4.5 ...
## $ r4 : num 4.5 24.5 0 0 14.5 14.5 4.5 4.5 0 4.5 ...
## $ r5 : num 14.5 14.5 0 0 14.5 14.5 24.5 4.5 0 4.5 ...
## $ r6 : num 24.5 14.5 24.5 0 4.5 14.5 24.5 14.5 0 44.5 ...
## $ r7 : num 24.5 14.5 0 0 0 4.5 14.5 14.5 34.5 14.5 ...
## $ r8 : num 14.5 4.5 44.5 0 0 4.5 4.5 14.5 14.5 4.5 ...
## $ r9 : num 4.5 4.5 24.5 34.5 0 14.5 4.5 4.5 4.5 4.5 ...
## $ r10 : num 4.5 14.5 24.5 45.5 24.5 14.5 14.5 14.5 24.5 4.5 ...
## $ mpaa : chr "" "" "" "" ...
## $ Action : int 0 0 0 0 0 0 1 0 0 0 ...
## $ Animation : int 0 0 1 0 0 0 0 0 0 0 ...
## $ Comedy : int 1 1 0 1 0 0 0 0 0 0 ...
## $ Drama : int 1 0 0 0 0 1 1 0 1 0 ...
## $ Documentary: int 0 0 0 0 0 0 0 1 0 0 ...
## $ Romance : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Short : int 0 0 1 0 0 0 0 1 0 0 ...
colnames(movies)
## [1] "title" "year" "length" "budget" "rating"
## [6] "votes" "r1" "r2" "r3" "r4"
## [11] "r5" "r6" "r7" "r8" "r9"
## [16] "r10" "mpaa" "Action" "Animation" "Comedy"
## [21] "Drama" "Documentary" "Romance" "Short"
head(movies)
## # A tibble: 6 x 24
## title year length budget rating votes r1 r2 r3 r4 r5
## <chr> <int> <int> <int> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 $ 1971 121 NA 6.4 348 4.5 4.5 4.5 4.5 14.5
## 2 $100~ 1939 71 NA 6 20 0 14.5 4.5 24.5 14.5
## 3 $21 ~ 1941 7 NA 8.2 5 0 0 0 0 0
## 4 $40,~ 1996 70 NA 8.2 6 14.5 0 0 0 0
## 5 $50,~ 1975 71 NA 3.4 17 24.5 4.5 0 14.5 14.5
## 6 $pent 2000 91 NA 4.3 45 4.5 4.5 4.5 14.5 14.5
## # ... with 13 more variables: r6 <dbl>, r7 <dbl>, r8 <dbl>, r9 <dbl>,
## # r10 <dbl>, mpaa <chr>, Action <int>, Animation <int>, Comedy <int>,
## # Drama <int>, Documentary <int>, Romance <int>, Short <int>
Elementos data y aesthetics
Cogemos la columna ratings para ver la frecuencia mediante un histograma. En primer lugar, creamos la variable pl correspondiente a la sintaxis básica en el paquete ggplot2 para la creación de una gráfica.
pl <- ggplot(movies, aes(x=rating))
Añadimos el elemento geométrico, en esta caso, queremos un histograma.
pl + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Podemos observar que la puntuación con mayor frecuencia anda rondando el 6.3 y que tanto el 0 como el 10 son las puntuaciones menos frecuentes.
Veamos cómo podemos modificar el histograma para que quede mejor visualmente.
pl2 <- pl + geom_histogram(binwidth = 0.1, col='black', fill='green', alpha=0.4)
pl2
binwidth nos permite configurar el ancho de cada barra. col indica el color del borde de cada barra y fill el color del interior. El comando alpha nos permite darle mayor o menor transparencia al gráfico.
Veamos ahora cómo añadir etiquetas.
pl2 + xlab('Puntuación') + ylab('count') + ggtitle('Contador de la puntuación de las películas')
Otra manera de rellenar las barras del histograma en función del número de apariciones es usando ..count.. en los aesthetics de geom_histogram()
pl + geom_histogram(binwidth=0.1, aes(fill=..count..), col='black')