Create your own theme
Load the ggplot2
package and customize your own theme. Look at the built-in themes and the ggthemes
package for inspiration.
Tips
You can save your customization using the theme()
, but that will only modify the current theme
library (ggplot2)
library (palmerpenguins)
mytheme <- theme (strip.background = element_rect (fill = "steelblue" ),
text = element_text (family = "Comic sans" ),
plot.background = element_rect (fill = "grey81" ),
legend.background = element_rect (fill = NA ),
legend.position = "bottom"
)
ggplot (penguins, aes (x = body_mass_g, y = bill_length_mm, color = species)) +
geom_point () + facet_wrap (~ year) + mytheme
Warning: Removed 2 rows containing missing values (geom_point).
theme_set (theme_bw ())
ggplot (penguins, aes (x = body_mass_g, y = bill_length_mm, color = species)) +
geom_point () + facet_wrap (~ year) + mytheme
Warning: Removed 2 rows containing missing values (geom_point).
To make a fully custom theme, start with an existing one, and modify it
my_fulltheme <- theme_grey () + mytheme
ggplot (penguins, aes (x = body_mass_g, y = bill_length_mm, color = species)) +
geom_point () + facet_wrap (~ year) + my_fulltheme
Warning: Removed 2 rows containing missing values (geom_point).
Save your favorite color scales as a function for easy reuse. Use discrete_scale
or continuous_scale
.
my_qual_scale <- function (...) {
discrete_scale ("color" , scale_name = "OI" ,
palette = function (x) {
res <- palette.colors (x, "Okabe-Ito" )[1 : x]
names (res) <- NULL
res
}, ...)
}
ggplot (penguins, aes (x = body_mass_g, y = bill_length_mm, color = species)) +
geom_point () + facet_wrap (~ year) + my_fulltheme + my_qual_scale ()
Warning: Removed 2 rows containing missing values (geom_point).