library(mncolors) # More information can be found in the vignette, including which palettes work best for those with reduced color vision. #```r # load the mncolors vignette # vignette("mncolors") #```
Use the mncolors
package to paint your data with colors from the MN state brand style
guide, and a
few color palettes inspired by local landscapes and animals, such as tree frogs and crayfish.
Use the palettes with ggplot2
or plot
to give your charts some MN style.
To install mncolors
:
# First install the 'remotes' package install.packages("remotes") # Now you can install mncolors from github remotes::install_github("MPCA-data/mncolors")
There are currently 13: primary, accent, extended, blue, green, gray, safety, primary_accent, primary_extended, primary_accent_extended, corn, treefrog, and caryfish.
library(mncolors) par(mfrow=c(round(0.1+length(mn_palettes)/2), 2), lheight = 2, mar=rep(1, 4), adj = 0) for (i in 1:length(mn_palettes)) viz_palette(mn_palettes[[i]], names(mn_palettes)[i])
More specifically:
mn_palettes
In a ggplot use the MN palettes with the scale_fill_mn()
and scale_color_mn()
functions.
library(mncolors) library(ggplot2) # Primary ggplot(data = mpg) + geom_point(aes(x = displ, y = hwy, color = class), size = 6, alpha = 0.7) + scale_color_mn(palette = "primary", reverse = TRUE) # Extended ggplot(data = mpg) + geom_point(aes(x = displ, y = hwy, color = class), size = 6, alpha = 0.7) + scale_color_mn(palette = "extended") # Primary + Accent ggplot(diamonds) + geom_bar(aes(x = cut, fill = clarity)) + scale_fill_mn(palette = "primary_accent") # Crayfish ggplot(diamonds) + geom_col(aes(y = mean(price), x = cut, fill = cut)) + scale_fill_mn(palette = "crayfish")
Alternatively, use mncolors()
to feed a specific number of colors from a palette to a ggplot layer.
library(ggplot2) df <- dplyr::starwars[1:5, ] ggplot(df, aes(x = height, y = reorder(name, height), fill = height)) + geom_col() + scale_fill_gradientn(colors = mncolors(5, palette = "primary")) + theme(legend.position = "none") + labs(title = "How tall are they?", subtitle = "Star Wars character heights", x = "height (cm)", y = "")
Enter any number you want to mncolors()
to return a bucket of colors.
mncolors(10, "blue")
# RColorBrewer code to demo the palettes n = 10 image( 1:n, 1, as.matrix(1:n), col = mncolors(n, palette = "blue"), xlab = "mncolors(blue)", ylab = "", xaxt = "n", yaxt = "n", bty = "n" )
mncolors(100, "green")
n = 100 image( 1:n, 1, as.matrix(1:n), col = mncolors(n, palette = "green"), xlab = "mncolors(green)", ylab = "", xaxt = "n", yaxt = "n", bty = "n" )
In this example we use the primary_accent
palette directly via the colorRampPalette()
function.
# Volcano example ## Create palette pal <- colorRampPalette(mn_palettes$primary_accent) ## Use 50 colors from it image(volcano, col = pal(50))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.