library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
library(ggplot2movies)
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>
En esta sección vamos a enfrentar dos variables del dataframe de las pelÃculas. Para ello usaremos el geom_bin2d(). En primer lugar, escribimos la sintaxis básica con el data y las variables.
pl <- ggplot(movies, aes(year, rating))
Y a continuación añadimos el elemento geométrico.
pl2 <- pl + geom_bin2d()
pl2
Podemos cambiar los colores.
pl2 + scale_fill_gradient(low='red', high='blue')
Y modificar el tamaño de los rectángulos para ver mejor el gradiente.
pl3 <- pl + geom_bin2d(binwidth=c(3,1)) + scale_fill_gradient(low="red",high="blue")
pl3
Podemos hacer la misma representación con hexágonos en lugar de rectángulos.
pl2 <- pl + geom_hex()
pl2
pl2 + scale_fill_gradient(low='red', high='blue')
pl2 <- pl + geom_density2d()
pl2