Skip to content

Instantly share code, notes, and snippets.

@moritzpschwarz
Last active November 19, 2023 14:09
Show Gist options
  • Save moritzpschwarz/a94ce04efb3bd1b0c8178ea76f828e01 to your computer and use it in GitHub Desktop.
Save moritzpschwarz/a94ce04efb3bd1b0c8178ea76f828e01 to your computer and use it in GitHub Desktop.
# base plot
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()
# Ensure that the legend is single line (e.g. at the bottom)
# I need to use guide_legend() because it is discrete
# use guide_colorbar() for continuous
ggplot(mtcars, aes(x = mpg, y = hp, color = cyl)) +
geom_point() +
guides(color = guide_legend(nrow = 1)) +
theme(legend.position = "bottom")
# Control the distance between the data and the panel border
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
scale_y_continuous(expand = expansion(mult = c(0.1, 0.2)))
# Use colours in the subtitle
library(ggtext)
library(MetBrewer)
# met.brewer("Cassatt1", 3) %>% as.vector()
ggplot(mtcars, aes(x = mpg, y = hp, color = as.factor(cyl))) +
geom_point() +
scale_color_met_d(name = "Cassatt1") +
labs(y = "Horsepower", x = "Miles per gallon", title = "Figure Title",
subtitle = "Comparing the <span style = color:#B1615C>4 Cylinder</span>, <span style = color:#E3ABA7>6 Cylinder</span> to <span style = color:#8282AA>8 Cylinder</span>") +
theme(plot.subtitle = ggtext::element_markdown(size = 15),
legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment