Skip to content

Instantly share code, notes, and snippets.

@dsparks dsparks/colors.R
Last active Sep 13, 2016

Embed
What would you like to do?
Sunlight Foundation palettes, based on the Wes Anderson code
# Sunlight Foundation style guide: http://design.sunlightlabs.com/projects/Sunlight-StyleGuide-DataViz.pdf
# Ram's original Wes Anderson code: https://github.com/karthik/wesanderson/blob/master/R/colors.R
#' A Wes Anderson palette generator
#'
#' These are a handful of color palettes from Wes Anderson movies.
#' @param n Number of colors desired. Unfortunately most palettes now only have 4 or 5 colors. But hopefully we'll add more palettes soon. All color schemes are derived from the most excellent Tumblr blog: \href{http://wesandersonpalettes.tumblr.com/}{Wes Anderson Palettes}
#' @param name Name of desired palette. Choices are: \code{GrandBudapest}, \code{Moonrise1}, \code{Royal1}, \code{Moonrise2}, \code{Cavalcanti}, \code{Royal2}, \code{GrandBudapest2}, \code{Moonrise3}, \code{Chevalier}, \code{BottleRocket}, \code{darjeeling}, \code{darjeeling2}
#' @param type Set to continuous if you require a gradient of colors similar to how heat map works.
#' @export
#' @keywords colors
#' @examples
#' sun.palette(3, "Royal1")
#' sun.palette(3, "GrandBudapest")
#' sun.palette(5, "Cavalcanti")
#' # You can also request a continuous range of colors
#' pal <- sun.palette(name = "Zissou", type = "continuous")
#' image(volcano, col = pal(21))
sun.palette <- function(n, name, type = FALSE) {
baseColors <- c("#EFECEA", # Background
"#F5F3F2", # Background Light Accent
"#E5E2E0", # Background Dark Accent
"#635F5D", # Text Main
"#8E8883", # Text Light
"#FFFFFF", # Line White
"#C0C0BB", # Line Grey Accent
"#E5E2E0") # No Data
# Data colors
mainColors <- c("#E3BA22", "#E6842A", "#137B80", "#8E6C8A", # Primaries
"#F2DA57", "#F6B656", "#42A5B3", "#B396AD", # Light subset
"#BD8F22", "#BA5F06", "#005D6E", "#684664", # Dark subset
"#978F80", "#C1BAA9", "#7C715E") # Neutral things
specialtyColors <- c("#9A3E25", # Republican
"#156B90", # Democrat
"#708259", # Independent
"#BD2D28", # Con
"#0F8C79", # Pro
"#5C8100") # Money
specialtySubsetColors <- c("#B37055", # Republican
"#688BAB", # Democrat
"#95A17E", # Independent
"#E25A42", # Con
"#6BBBA1", # Pro
"#A0B700") # Money
# Choropleth colors
republicanRange <- c("#712422", "#9A3E25", "#B37055", "#D9A78D", "#EDCEBC")
democratRange <- c("#05426C", "#156B90", "#688BAB", "#8CAEC6", "#BAD2E2")
repToDemRange <- c("#9A3E25", "#D9A78D", "#C8C6C6", "#8CAEC6", "#156B90")
antiToProRange <- c("#BD2D28", "#C37A73", "#C8C6C6", "#75A999", "#0F8C79")
moneyRange <- c("#0C4E00", "#5C8100", "#A0B700", "#D2CF00", "#E6E4A6")
thingRange <- c("#936B00", "#BD8F22", "#E3BA22", "#F2DA57", "#F1E8AE")
networkGraphColors <- c("#F2DA57", "#F6B656", "#E25A42", "#DCBDCF", "#B396AD", # Main
"#B0CBDB", "#33B6D0", "#7ABFCC", "#C8D7A1", "#A0B700",
"#E3BA22", "#E58429", "#BD2D28", "#D15A86", "#8E6C8A", # Lighter
"#6B99A1", "#42A5B3", "#0F8C79", "#6BBBA1", "#5C8100",
"#B08B12", "#BA5F06", "#8C3B00", "#6D191B", "#842854", # Darker
"#5F7186", "#193556", "#137B80", "#144847", "#254E00")
hclSweep <- hcl(h = seq(0, 255, len = 100),
c = seq(100, 0, len = 100),
l = seq(100, 0, len = 100))
namelist <- ls()
print(namelist)
if(!name %in% namelist)
stop("Palette not found.")
if(type == "continuous") {
colorRampPalette(get(name), space="Lab")
} else {
if(!type) {
if(n > namelist[which(namelist == name)])
stop("Number of requested colors greater than what palette can offer")
get(name)[1:n]
}
}
}
#' Display a palette
#'
#' @param n Number of colors desired. Unfortunately most palettes now only have 4 or 5 colors. But hopefully we'll add more palettes soon. All color schemes are derived from the most excellent Tumblr blog: \href{http://wesandersonpalettes.tumblr.com/}{Wes Anderson Palettes}
#' @param name Name of desired palette. Choices are: \code{GrandBudapest}, \code{Moonrise1}, \code{Royal1}, \code{Moonrise2}, \code{Cavalcanti}, \code{Royal2}, \code{GrandBudapest2}, \code{Moonrise3}, \code{Chevalier} , \code{BottleRocket} , \code{darjeeling}, \code{darjeeling2}
#' @export
#' @examples \dontrun{
#' display.sun.palette(3, "Royal1")
#'}
display.sun.palette <- function(n, name) {
if(!name %in% namelist)
stop("Palette not found.")
if(n > namelist[which(namelist == name)])
stop("Number of requested colors greater than what palette can offer")
image(1:n,1,as.matrix(1:n),col= sun.palette(n,name),
xlab=name, ylab = "",xaxt = "n",yaxt = "n", bty = "n")
}
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3) +
scale_color_manual(values = sun.palette(3, "moneyRange")) +
theme_gray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.