Created
July 8, 2020 04:47
-
-
Save jrosell/ab09f9ce188ae7956ffc80f27f35100d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(!require(tidyverse)) install.packages("tidyverse") | |
if(!require(patchwork)) install.packages("patchwork") | |
library(patchwork) | |
library(tidyverse) | |
library(lubridate) | |
# Añadir anotaciones | |
ggplot(mpg, aes(displ, hwy)) + | |
geom_point(aes(color = class)) + | |
geom_smooth(se = FALSE) + labs( | |
title = "Fuel efficiency generally decreases with engine size", | |
subtitle = "Two seaters (sports cars) are an exception because of their light weight", | |
caption = "Data from fueleconomy.gov", | |
x = "Engine displacement (L)", | |
y = "Highway fuel economy (mpg)", | |
colour = "Car type" | |
) | |
# Ajustando etiquetas | |
library(ggrepel) | |
label <- mpg %>% summarise( displ = max(displ), hwy = max(hwy), label = "Increasing engine size is \nrelated to decreasing fuel economy.") | |
best_in_class <- mpg %>% group_by(class) %>% filter(row_number(desc(hwy)) == 1) | |
ggplot(mpg, aes(displ, hwy)) + | |
geom_point(aes(colour = class)) + | |
geom_point(size = 3, shape = 1, data = best_in_class) + | |
geom_text(aes(label = label), data = label, vjust = "top", hjust = "right") + | |
geom_label_repel(aes(label = model), data = best_in_class) | |
# Accesibilidad | |
ggplot(mpg, aes(displ, hwy)) + | |
geom_point(aes(color = drv, shape = drv)) + # shapes ensure your plot is interpretable in black and white | |
scale_colour_brewer(palette = "Set1") + | |
theme(legend.position = "bottom") | |
# Compartir escala | |
suv <- mpg %>% filter(class == "suv") | |
compact <- mpg %>% filter(class == "compact") | |
x_scale <- scale_x_continuous(limits = range(mpg$displ)) | |
y_scale <- scale_y_continuous(limits = range(mpg$hwy)) | |
col_scale <- scale_colour_discrete(limits = unique(mpg$drv)) | |
p1 <- ggplot(suv, aes(displ, hwy, colour = drv)) + geom_point() + x_scale + y_scale + col_scale + theme(legend.position = "none") | |
p2 <- ggplot(compact, aes(displ, hwy, colour = drv)) + geom_point() + x_scale + y_scale + col_scale | |
p1 + p2 | |
# Estilo | |
ggplot(mpg, aes(displ, hwy)) + | |
geom_point(aes(color = class)) + | |
geom_smooth(se = FALSE) + | |
theme_bw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment