Some base layers to see how the paletes work:
# World map
world_map <- rnaturalearth::ne_countries(scale = 'small', returnclass = c("sf"))
# Base map
world_map.s <- ggplot() +
geom_sf(data = world_map, size = .2, col="grey90", aes(fill=admin), show.legend = FALSE) +
theme(panel.grid.major = element_line(color = gray(0.95), linetype = "dashed", size = 0.5), legend.position="none")+
xlab("Longitude")+
ylab("Latitude")+
theme_minimal()
# Base bar plot
g1 <- ggplot(mtcars) +
geom_bar(aes(x=factor(hp), fill=factor(hp)))
par(mfrow=c(2,2), mar=c(4,4,1,1))
barplot(1:5, col=rainbow(5))
# Use heat.colors
barplot(1:5, col=heat.colors(5))
# Use terrain.colors
barplot(1:5, col=terrain.colors(5))
# Use topo.colors
barplot(1:5, col=topo.colors(5))
par(mfrow=c(1,1), mar=c(4,4,1,1))
library(RColorBrewer)
# display.brewer.pal(11, "BrBG")
brbg <- brewer.pal(11, "BrBG")
cols <- c(colorRampPalette(c(brbg[1], brbg[6]))(51),
colorRampPalette(c(brbg[6], brbg[11]))(51)[-1])
par(mar=c(1,5,1,1))
display.brewer.all()
# Example:
g1 + scale_fill_brewer(palette = "Spectral")
world_map.s + scale_fill_brewer(palette = "Spectral")
# expand palette:
# https://www.r-bloggers.com/how-to-expand-color-palette-with-ggplot-and-rcolorbrewer/
world_map.s +
scale_fill_manual(values = colorRampPalette(brewer.pal(12, "Spectral"))(177))
world_map.s +
scale_fill_manual(values = colorRampPalette(brewer.pal(12, "Accent"))(177))
require(viridis)
# scale_color_viridis(discrete=TRUE)
# scale_color_viridis(discrete=FALSE)
# scale_fill_viridis(discrete=TRUE)
data("geyser", package="MASS")
ggplot(geyser, aes(x = duration, y = waiting)) +
xlim(0.5, 6) + ylim(40, 110) +
stat_density2d(aes(fill = ..level..), geom="polygon") +
theme_bw() +
theme(panel.grid=element_blank()) -> gg
grid.arrange(
gg + scale_fill_viridis(option="A") + labs(x="Virdis A", y=NULL),
gg + scale_fill_viridis(option="B") + labs(x="Virdis B", y=NULL),
gg + scale_fill_viridis(option="C") + labs(x="Virdis C", y=NULL),
gg + scale_fill_viridis(option="D") + labs(x="Virdis D", y=NULL),
gg + scale_fill_viridis(option="E") + labs(x="Virdis E", y=NULL),
ncol=3, nrow=2
)
world_map.s + scale_fill_viridis(discrete = TRUE)
world_map.s + scale_fill_viridis(discrete = TRUE, option="plasma")
Ochre is a brownish-yellow pigment that occurs naturally in soils across Australia. Historically, ochre has been used for artwork by many indigenous cultures, including the Aboriginal people of Australia.
The goal of ochRe is to provide colour palettes inspired by Australian art, landscapes and wildlife.
# You need to install the 'devtools' package first
# devtools::install_github("ropenscilabs/ochRe")
require(ochRe)
# I did not managed to instal this package for the moment.
# package ‘ochRe’ is not available (for R version 3.6.1)
pal_names <- names(ochre_palettes)
par(mfrow=c(length(ochre_palettes)/2, 2), lheight = 2, mar=rep(1, 4), adj = 0)
for (i in 1:length(ochre_palettes)){
viz_palette(ochre_palettes[[i]], pal_names[i])
}
## basic example code
par(mfrow=c(1,1))
pal <- colorRampPalette(ochre_palettes[["winmar"]])
image(volcano, col = pal(20))
## ggplot:
g1 + scale_fill_ochre()
world_map.s + scale_fill_ochre()
library(wesanderson);
# for ggplot:
# scale_color_manual(values = wes_palette("GrandBudapest1"))
par(mfrow=c(3,4))
wes_palette("Royal1")
wes_palette("GrandBudapest1")
wes_palette("Cavalcanti1")
wes_palette("Cavalcanti1", 3)
wes_palette("BottleRocket1")
wes_palette("BottleRocket2")
wes_palette("Rushmore1")
wes_palette("Zissou1")
wes_palette("IsleofDogs1")
wes_palette("IsleofDogs2")
wes_palette("Moonrise1")
wes_palette("Moonrise2")
par(mfrow=c(1,1))
# If you need more colours than normally found in a palette, you
# can use a continuous palette to interpolate between existing
# colours
pal <- wes_palette(21, name = "Zissou1", type = "continuous")
image(volcano, col = pal)
pal <- wes_palette(177, name = "Zissou1", type = "continuous")
world_map.s + scale_fill_manual(values = pal)
Color Palettes for ggplot2 (ggsci)
You will be able to use the generated hex color codes for such graphics systems accordingly. The transparent level of the entire palette is easily adjustable via the argument “alpha” in every generator or scale function.
library(ggsci);library("scales")
# List functions from the library
kk <- ls("package:ggsci")
# Select the functions with paletes from that library:
kk <- kk[substr(kk,1,3)=="pal"]
# How many plots in the window?
length(kk)
## [1] 18
# Now, plot all palettes with color codes
par(mfrow=c(5,4), mar=c(0,0,1,0))
for(ii in 1:length(kk)){
funcList <- as.list(kk)
f <- get(kk[[ii]])
show_col(f()(7), labels=FALSE)
title(kk[ii])
}
# For GGPLOT:
# scale_color_palname()
# scale_fill_palname()
# Example:
g1 +
scale_fill_npg()
g1 +
scale_fill_nejm()
g1 +
scale_fill_simpsons()
g1 +
scale_fill_locuszoom()
mypal = pal_npg("nrc", alpha = 0.7)(9)
mypal
## [1] "#E64B35B2" "#4DBBD5B2" "#00A087B2" "#3C5488B2" "#F39B7FB2" "#8491B4B2"
## [7] "#91D1C2B2" "#DC0000B2" "#7E6148B2"
show_col(mypal)
# adapted from: https://stackoverflow.com/questions/37482977/what-is-a-good-palette-for-divergent-colors-in-r-or-can-viridis-and-magma-b
# install.packages("devtools")
# devtools::install_github("thomasp85/scico")
library(scico)
scico_palette_show()
scico_palette_show(palettes = c("broc", "cork", "vik",
"lisbon", "tofino", "berlin",
"batlow", "roma"))
# example of barplots:
g1 +
scale_fill_scico_d(palette = 'lisbon')
g1 +
scale_fill_scico_d(palette = 'tokyo')
g1 +
scale_fill_scico_d(palette = 'roma')
world_map.s +
scale_fill_scico_d(palette = 'roma')
Palettes from movies (nemo, up, etc.).
library(yarrr)
par(mfrow=c(1,1))
piratepal(palette = "all")
Another great package is cmocean (Python). Its colormaps are available in R via the pals package or the oce package.
Paper: Thyng, K. M., Greene, C. A., Hetland, R. D., Zimmerle, H. M., & DiMarco, S. F. (2016). True colors of oceanography. Oceanography, 29(3), 10, http://dx.doi.org/10.5670/oceanog.2016.66. Talk: PLOTCON 2016: Kristen Thyng, Custom Colormaps for Your Field.
### install.packages("devtools")
### devtools::install_github("kwstat/pals")
library(pals)
pal.bands(ocean.balance, ocean.delta, ocean.curl, main = "cmocean")
g1 + scale_fill_manual(values=as.vector(ocean.balance(26)))
g1+ scale_fill_manual(values=as.vector(ocean.delta(26)))
g1 + scale_fill_manual(values=as.vector(polychrome(26)))
world_map.s + scale_fill_manual(values=as.vector(ocean.delta(177)))
world_map.s + scale_fill_manual(values=as.vector(ocean.balance(177)))
# https://towardsdatascience.com/using-the-new-turbo-palette-from-google-in-r-af19b9424cc0
pal.bands(alphabet, alphabet2, cols25, glasbey, kelly, okabe, polychrome, tableau20, tol, watlington)
pal.bands(stepped, stepped2, stepped3)
pal.bands(tol.groundcover)
## Not run:
alphabet()
## amethyst blue caramel damson ebony forest green
## "#F0A0FF" "#0075DC" "#993F00" "#4C005C" "#191919" "#005C31" "#2BCE48"
## honeydew iron jade khaki lime magenta navy
## "#FFCC99" "#808080" "#94FFB5" "#8F7C00" "#9DCC00" "#C20088" "#003380"
## orange pink quagmire red sky turquoise uranium
## "#FFA405" "#FFA8BB" "#426600" "#FF0010" "#5EF1F2" "#00998F" "#E0FF66"
## violet wine xanthin yellow zinnia
## "#740AFF" "#990000" "#FFFF80" "#FFE100" "#FF5005"
alphabet()["jade"]
## jade
## "#94FFB5"
pal.bands(alphabet,n=26)
pal.heatmap(alphabet)
pal.heatmap(alphabet2)
pal.heatmap(cols25)
pal.heatmap(glasbey())
pal.heatmap(kelly()) # too many orange/pink colors
pal.safe(okabe()) # not great
pal.heatmap(polychrome)
pal.heatmap(stepped, n=24)
pal.heatmap(stepped2, n=20)
pal.heatmap(stepped3, n=20)
pal.heatmap(tol, 12)
pal.heatmap(watlington(16))
The choices of color palettes in R can be quite overwhelming with palettes spread over many packages with many different API’s. This packages aims to collect all color palettes across the R ecosystem under the same package with a streamlined API.
The help file contains a comprehesive list of differetn palettes in r.
require(marmap)
etopo.colors(10)
## [1] "#FBFBFB" "#935B5B" "#7F4A1D" "#917111" "#AD8913" "#D3AA52" "#B0B076"
## [8] "#4C8B52" "#9CC4E4" "#090B6A"
# scale_fill_etopo(...)
# scale_color_etopo(...)
# From the example file:
# plot with base graphics
getNOAA.bathy(lon1=-12, lon2=-1,lat1=34,lat2=45, resolution=1) -> a
plot(a, image=TRUE, deep=-6000, shallow=0, step=1000)
# using the etopo color scale
etopo_cols <- rev(etopo.colors(8))
plot(a, image=TRUE, bpal=list(
c(min(a), 0, etopo_cols[1:2]),
c(0, max(a), etopo_cols[3:8])
))
# using ggplot
ggplot(a, aes(x=x, y=y)) + coord_quickmap() +
# background
geom_raster(aes(fill=z)) +
scale_fill_etopo() +
# countours
geom_contour(aes(z=z),
breaks=c(0, -100, -200, -500, -1000, -2000, -4000),
colour="black", size=0.2
) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0))
require(oceanmap)
par(mar=c(3, 2, 2, 2))
site <- extent(-10,-6,36,39)
plotmap(site, grid=F, main="Portugal")
bathy <- get.bathy(site, grid=F, cbpos='r', resolution=3)
## loading bathymetry data at a resolution of 3 minutes
## Querying NOAA database ...
## This may take seconds to minutes, depending on grid size
##
## no region (v_area) defined, run add.region to add region definitions and to save settings for the plot region, colorbar and window size!
##
## printing file: lon-10--6.lat36-39_bathy