View source: R/theme_grattan.R
theme_grattan | R Documentation |
Create a ggplot2 theme consistent with the Grattan style guide.
theme_grattan(
base_size = 18,
base_family = "sans",
chart_type = "normal",
flipped = FALSE,
background = "white",
legend = "none",
panel_borders = FALSE
)
base_size |
Size for text elements. Defaults to 18, as per the Grattan style guide. |
base_family |
Font family for text elements. Defaults to "sans". |
chart_type |
"normal" by detault. Set to "scatter" for scatter plots. |
flipped |
FALSE by default. Set to TRUE if using coord_flip(). If set to TRUE, the theme will show a vertical axis line, ticks & panel grid, while hiding the horizontals. Ignored for type = "scatter". |
background |
"white" by default. Set to "orange" or "box" if you're making a chart to go in a Grattan report box. |
legend |
"off" by default. Set to "bottom", "left", "right" or "top" as desired, or a two element numeric vector such as c(0.9, 0.1). |
panel_borders |
'FALSE' by default. Set to 'TRUE' to enable a black border around the plotting area. |
# This function goes most of the way to making your charts Grattan-y,
# but manual tweaking will almost definitely be required
# A minimal example:
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_grattan()
# To ensure your x-axis is at zero (or some other value you choose),
# you may need to manually tweak the scale of the y-axis.
# Use scale_y_continuous_grattan() for some sensible default values, which
# may need further tweaking.
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
scale_y_continuous_grattan() +
theme_grattan()
# You'll notice in the example above that the top of the chart now looks
# good; the bottom has two
# points that are half hanging off the axis. Try the following, substituing
# any value (incl. 0) for 10 as you like:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
theme_grattan()
# An example with colours follows. See ?grattan_pal for more information and
# options.
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan()
# The legend is off by default. You may wish to turn it on. Here's how:
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan(legend = "bottom")
# The flipped = TRUE option makes things easier when using coord_flip, as in:
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan(flipped = TRUE, legend = "bottom") +
coord_flip()
# Making a chart to go in a box? Then you'll want the background = "orange"
# option, as in:
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan(flipped = TRUE, background = "orange") +
theme(legend.position = "bottom") +
coord_flip()
# Want to make a 'self-contained' chart that includes a title/subtitle/caption,
# eg. to go on the Grattan Blog? If so, just add them - they'll be properly
# left-aligned when you save them with grattan_save(), like this:
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
geom_point() +
scale_y_continuous_grattan(limits = c(10, NA)) +
scale_colour_manual(values = grattan_pal(n = 3)) +
theme_grattan() +
labs(title = "Title goes here",
subtitle = "Subtitle goes here",
caption = "Notes: Notes go here\nSource: Source goes here")
# Save the plot with:
## Not run: grattan_save("your_file.png")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.