knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(ggplot2) library(ggprism) library(patchwork)
ggprism
includes colour and fill scales with various palettes that mirror
the colour schemes available in GraphPad Prism.
Disclaimer: some of these palettes are
quite nice, some are pretty ugly and not useful, and most are not colour blind
friendly as far as I know (except palette = "colorblind_safe"
). It would
probably be safer to use other colour scales e.g. those in RColorBrewer
for
any serious application or work to be published.
In any case, these scales are used to colour the
data elements of ggplots and can be used separately or together with
theme_prism()
. Additionally, a shape scale with 3 palettes is included which
is similar to the shapes available in Prism. This vignette outlines how to
use scale_colour_prism()
(or scale_color_prism()
), scale_fill_prism()
,
and scale_shape_prism()
.
First we'll create a base plot.
# create a base plot to compare colour scales base <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(aes(colour = factor(cyl), shape = factor(cyl)), size = 3) + theme_prism() + guides(colour = guide_legend(position = "inside")) + theme(legend.position.inside = c(0.8, 0.8)) base
scale_colour_prism()
(US spelling also works) functions pretty much
identically to scale_colour_manual()
from ggplot2
. The default palette for
scale_colour_prism()
is "colors"
.
# compare manual colour scale with prism colour scale p1 <- base + scale_colour_manual(values = c("blue", "red", "green3")) p2 <- base + scale_colour_prism() p1 + p2
There are several different colour palettes that are built into
scale_colour_prism()
.
# see names and lengths of available scale_colour_prism() palettes lengths(ggprism_data$colour_palettes)
Using the palettes is simple.
# try out some different colour palettes p1 <- base + scale_colour_prism(palette = "purple_passion") p2 <- base + scale_colour_prism(palette = "candy_bright") p1 + p2
You can preview a single palette using the preview_theme()
function. It shows
a sample plot with the colour scale applied (as well as scale_fill_prism()
and
theme_prism()
). See
this page for
images of most available colour palettes.
preview_theme("flames")
First we'll create a base plot.
# create a base plot to compare fill scales base <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(aes(fill = factor(cyl), shape = factor(cyl)), size = 3) + theme_prism() + guides(fill = guide_legend(position = "inside"), shape = guide_legend(position = "inside")) + theme(legend.position.inside = c(0.8, 0.8)) + scale_shape_prism(palette = "filled") base
scale_fill_prism()
functions pretty much
identically to scale_fill_manual()
from ggplot2
. The default palette for
scale_fill_prism()
is "colors"
.
# compare manual fill scale with prism fill scale p1 <- base + scale_fill_manual(values = c("blue", "red", "green3")) p2 <- base + scale_fill_prism() p1 + p2
There are several different fill palettes that are built into
scale_fill_prism()
.
# see names and lengths of available scale_fill_prism() palettes lengths(ggprism_data$fill_palettes)
Using the palettes is simple.
# try out some different fill palettes p1 <- base + scale_fill_prism(palette = "colorblind_safe") p2 <- base + scale_fill_prism(palette = "neon") p1 + p2
You can preview a single palette using the preview_theme()
function. It shows
a sample plot with the colour scale applied (as well as scale_colour_prism()
and
theme_prism()
). See
this page for
images of most available fill palettes.
preview_theme("diazo")
The order of shapes used in GraphPad Prism is slightly different to ggplot2
,
hence 3 different shape palettes are included in the ggprism
function
scale_shape_prism()
.
We can see the names and lengths of these shape palettes. Naturally,
"default"
is the default palette and does not include any shape with a fill.
The "filled"
palette should be used when you also want to use a fill scale,
as the first 5 symbols have a fill. The "complete"
palette symbols 5-9 have
a fill.
# see names and lengths of available scale_shape_prism() palettes lapply(ggprism_data$shape_palettes, nrow)
We can also define a function to see all the shapes in a specific palette.
# define a function for convenience show_shapes <- function(palette) { df_shapes <- ggprism_data$shape_palettes[[palette]][, -1] df_shapes$pch_f <- factor(df_shapes$pch, levels = df_shapes$pch) ggplot(df_shapes, aes(x = 0, y = 0, shape = pch)) + geom_point(aes(shape = pch), size = 5, fill = 'red') + scale_shape_identity() + facet_wrap(~ pch_f) + theme_void() } # show the shapes in the palette "complete" show_shapes("complete")
We'll define a base plot to see scale_shape_prism()
in action.
# create a base plot to compare shape scales base <- ggplot(mpg, aes(x = displ, y = cty)) + geom_point(aes(colour = class, fill = class, shape = class)) + theme_prism(base_size = 11, base_fontface = "plain", border = TRUE) + guides(colour = guide_legend(position = "inside"), fill = guide_legend(position = "inside"), shape = guide_legend(position = "inside")) + theme(plot.subtitle = element_text(face = "bold"), legend.position.inside = c(0.8, 0.75), legend.key.height = unit(10, "pt")) + coord_cartesian(clip = "off") + scale_colour_prism(palette = "floral") + scale_fill_prism(palette = "floral") base
Then we'll compare the 3 shape palettes to the ggplot2
base plot.
# compare shape scales p1 <- base p2 <- base + scale_shape_prism(palette = "default") + labs(subtitle = "default") p3 <- base + scale_shape_prism(palette = "filled") + labs(subtitle = "filled") p4 <- base + scale_shape_prism(palette = "complete") + labs(subtitle = "complete") (p1 + p2) / (p3 + p4)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.