The colour
argument changes the point and line colour, while the fill
argument changes the interior colour of shapes. Type colours()
into the console to see a list of all the named colours in R. Alternatively, you can use hexadecimal colours like "#FF8000"
or the rgb()
function to set red, green, and blue values on a scale from 0 to 1.
Hover over a colour to see its R name.
.colordemo { list-item-style: none; padding: 0; margin: 0;} .colordemo li, main ul.colordemo > li:first-child { display: inline-block; border: 0.25px solid black; margin: 0px; padding: 1.3em 0.2em 0.2em 1.2em; } .colordemo li span { display: none; background-color: white; border: 0.25px solid black; padding: 0.25em 0.5em; border-radius: 0 1em 1em 1em; } .colordemo li:hover span { display: inline; position: absolute; }The alpha
argument changes transparency (0 = totally transparent, 1 = totally opaque).
(\#fig:alpha-demo)Varying alpha values.
The shape
argument changes the shape of points.
(\#fig:shape-demo)The 25 shape values
You can probably guess what the linetype
argument does.
(\#fig:linetype-demo)The 6 linetype values at different sizes.
Discrete palettes change depending on the number of categories.
(\#fig:unnamed-chunk-3)Default discrete palette with different numbers of levels.
Viridis palettes are very good for colourblind-safe and greyscale-safe plots. The work with any number of categories, but are best for larger numbers of categories or continuous colours.
Set discrete viridis colours with scale_colour_viridis_d()
or scale_fill_viridis_d()
and set the option
argument to one of the options below. Set direction = -1
to reverse the order of colours.
(\#fig:unnamed-chunk-4)Discrete viridis palettes.
::: {.info data-latex=""}
If the end colour is too light for your plot or the start colour too dark, you can set the begin
and end
arguments to values between 0 and 1, such as scale_colour_viridis_c(begin = 0.1, end = 0.9)
.
:::
Set continuous viridis colours with scale_colour_viridis_c()
or scale_fill_viridis_c()
and set the option
argument to one of the options below. Set direction = -1
to reverse the order of colours.
(\#fig:unnamed-chunk-5)Continuous viridis palettes.
Brewer palettes give you a lot of control over plot colour and fill. You set them with scale_color_brewer()
or scale_fill_brewer()
and set the palette
argument to one of the palettes below. Set direction = -1
to reverse the order of colours.
These palettes are good for categorical data with up to 8 categories (some palettes can handle up to 12). The "Paired" palette is useful if your categories are arranged in pairs.
(\#fig:unnamed-chunk-6)Qualitative brewer palettes.
These palettes are good for up to 9 ordinal categories with a lot of categories.
(\#fig:unnamed-chunk-7)Sequential brewer palettes.
These palettes are good for ordinal categories with up to 11 levels where the centre level is a neutral or baseline category and the levels above and below it differ in an important way, such as agree versus disagree options.
(\#fig:unnamed-chunk-8)Diverging brewer palettes.
ggplot2
has 8 built-in themes that you can add to a plot like plot + theme_bw()
or set as the default theme at the top of your script like theme_set(theme_bw())
.
(\#fig:unnamed-chunk-9){ggplot2} themes.
You can get more themes from add-on packages, like ggthemes
. Most of the themes also have custom scale_
functions like scale_colour_economist()
. Their website has extensive examples and instructions for alternate or dark version sof these themes.
(\#fig:unnamed-chunk-10){ggthemes} themes.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.