wikitopo_scale: Wikipedia topographic scale and palette

View source: R/scale_wikitopo.R

wikitopo_scaleR Documentation

Wikipedia topographic scale and palette

Description

Color scale and palette from Wikipedia's topographic maps. These scales are special because the mapping between colors and values is predefined to work for altitudes (or depths when the numbers are negative).

Usage

wikitopo_scale(na.value = NULL)

wikitopo_map(x, ...)

wikitopo_palette(...)

wikitopo_colors(n, ...)

scale_fill_wikitopo(na.value = NULL, ...)

scale_color_wikitopo(na.value = NULL, ...)

Arguments

na.value

value to return for missing values in the input. Can be either a color, NULL in which case a tentitatively appropriate color will be chosen automatically, or NA.

x

a vector whose values will be coerced to numbers and mapped to colors.

...

passed to wikitopo_scale or to scale_*_gradientn for the scale_*_wikitopo functions.

n

number of colors to extract from the color palette.

Value

*_scale returns a function. This function takes a single argument (x: a numeric vector), maps its values to colors, and returns thee colors as hex codes.

*_map is a shortcut for *_scale(domain=range(x))(x): it creates a scale that spans the range of values in argument x, maps the content of x on that scale, and returns the colors.

*_palette returns a function. This function takes an integer (n) as argument, picks n colors evenly spaced along the scale, and returns them as hex codes.

*_colors is a shortcut for *_palette()(n) and directly returns n evenly spaced colors. It is equivalent to built-in functions such as heat.colors, topo.colors, etc.

scale_* return a ggplot2 scale, either discrete (similar to scale_color_discrete) or continuous (similar to scale_color_continuous).

References

https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Maps/Conventions/Topographic_maps.

See Also

Other color scales and palettes: brewer_scale(), chroma_scale(), cubehelix_scale(), etopo_scale(), hue_scale(), inferno_scale(), interp_scale(), light_scale(), magma_scale(), plasma_scale(), turbo_scale(), viridis_scale()

Examples

# Defining a scale in that case has little value since it cannot be
# customized. Explore the default mapping
altitudes <- c(-5000, -1000, -10, 0, 10, 200, 500, 5000)
show_col(wikitopo_map(altitudes))
# Explore the full range of colors
show_col(wikitopo_map(seq(-8000, 8000, length=500)))

# It is possible to get n colors from the palette but they loose their
# association with specific altitudes
wikitopo_colors(5)

# The goal of these scales is to color maps and elevation models. Here is
# one centered on Thailand, showcasing high mountains and deep trenches
levs <- seq(-9000, 6000, by=500)
contour(thai, levels=levs, col=wikitopo_map(levs), asp=1.03)

filled.contour(thai, levels=levs, col=wikitopo_map(levs), asp=1.03)

persp(thai, theta=30, phi=25, border=alpha("black", 0.2), expand=0.2,
      col=wikitopo_map(persp_facets(thai$z)))

## Not run: 
# in spinning 3D
library("rgl")
persp3d(thai, aspect=c(1,0.96,0.2), axes=FALSE, box=FALSE,
        col=wikitopo_map(thai$z))
play3d(spin3d(axis=c(0, 0, 1), rpm=10), duration=6)

# or with ggplot2
library("ggplot2")
ggplot(thaixyz) + coord_quickmap() +
  geom_contour(aes(x, y, z=z, color=..level..), breaks=levs) +
  theme_light() + scale_color_wikitopo() + scale_xy_map()
ggplot(thaixyz) + coord_quickmap() +
  geom_raster(aes(x, y, fill=z)) +
  scale_fill_wikitopo() + scale_xy_map() +
  geom_contour(aes(x, y, z=z), breaks=0, color="black") 
## End(Not run)

jiho/chroma documentation built on Nov. 26, 2022, 2:39 a.m.