# NOT RUN {
# *** Examples for sequential CARTO scales ***
# base plot
library("ggplot2")
gg <- ggplot(iris, aes(x = Species, y = Sepal.Width, color = Sepal.Length)) +
geom_jitter(width = 0.3) + theme_minimal()
# default settings
gg + scale_color_continuous_divergingx()
# switch palette to ArmyRose
gg + scale_color_continuous_divergingx(palette = "ArmyRose")
# select a range out of the entire palette
gg + scale_color_continuous_divergingx(palette = "ArmyRose", begin = .2, end = .8)
# volcano plot
nx = 87
ny = 61
df <- data.frame(height = c(volcano), x = rep(1:nx, ny), y = rep(1:ny, each = nx))
ggplot(df, aes(x, y, fill=height)) +
geom_raster() + scale_fill_continuous_divergingx(palette = "Fall", rev = TRUE) +
coord_fixed(expand = FALSE)
# *** Examples for diverging CARTO scales ***
# adapted from stackoverflow: https://stackoverflow.com/a/20127706/4975218
# generate dataset and base plot
library("ggplot2")
set.seed(100)
df <- data.frame(country = LETTERS, V = runif(26, -40, 40))
df$country = factor(LETTERS, LETTERS[order(df$V)]) # reorder factors
gg <- ggplot(df, aes(x = country, y = V, fill = V)) +
geom_bar(stat = "identity") +
labs(y = "Under/over valuation in %", x = "Country") +
coord_flip() + theme_minimal()
# plot with diverging scale "Geyser"
gg + scale_fill_continuous_divergingx(palette = "Geyser")
# plot with diverging scale "ArmyRose"
gg + scale_fill_continuous_divergingx(palette = "ArmyRose")
# }
Run the code above in your browser using DataCamp Workspace