colormap

[This article was first published on Dan Kelley Blog/R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Over the past month or so I have been trying different ways of handling GMT-style colormaps in Oce. I think my present solution is on the right track, but I am posting here to get more eyes on the problem.

Note that the function called Colormap() here will be called colormap() if I decided to incorporate it into Oce. That will mean that it replaces an old function of the same name. Also, as part of the change, the old function colorize() will disappear. (To call them “old” is a stretch. They are about a month old and were marked as “Alpha” code from the start.)

Procedure

The following code is direct from the help for Colormap(); all I’ve done is to put the example code into Rmarkdown to make for easier comparision with the resultant graphs.

1
library(oce)
## Loading required package: methods
## Loading required package: mapproj
## Loading required package: maps
## Loading required package: ncdf4
## Loading required package: tiff
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## Example 1. color scheme for points on xy plot
x <- seq(0, 1, length.out = 40)
y <- sin(2 * pi * x)
par(mar = c(3, 3, 1, 1))
mar <- par("mar")  # prevent margin creep by drawPalette()
## First, default breaks
c <- Colormap(y)
drawPalette(c$zlim, col = c$col, breaks = c$breaks)
plot(x, y, bg = c$zcol, pch = 21, cex = 1)
grid()

center

1
2
3
4
5
6
par(mar = mar)
## Second, 100 breaks, yielding a smoother palette
c <- Colormap(y, breaks = 100)
drawPalette(c$zlim, col = c$col, breaks = c$breaks)
plot(x, y, bg = c$zcol, pch = 21, cex = 1)
grid()

center

1
2
3
4
5
6
7
par(mar = mar)

## Example 2. topographic image with a standard color scheme
par(mfrow = c(1, 1))
data(topoWorld)
cm <- Colormap(name = "gmt_globe")
imagep(topoWorld, breaks = cm$breaks, col = cm$col)

center

1
2
3
4
5
6
7
## Example 3. topographic image with modified colors
cm <- Colormap(name = "gmt_globe")
deep <- cm$x0 < -4000
cm$col0[deep] <- "black"
cm$col1[deep] <- "black"
cm <- Colormap(x0 = cm$x0, x1 = cm$x1, col0 = cm$col0, col1 = cm$col1)
imagep(topoWorld, breaks = cm$breaks, col = cm$col)

center

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## Example 4. image of world topography with water colorized smoothly from
## violet at 8km depth to blue at 4km depth, then blending in 0.5km
## increments to white at the coast, with tan for land.
cm <- Colormap(x0 = c(-8000, -4000, 0, 100), x1 = c(-8000, -4000, 0, 100), col0 = c("violet", 
    "blue", "white", "tan"), col1 = c("violet", "blue", "white", "tan"), n = c(100, 
    8, 1))
lon <- topoWorld[["longitude"]]
lat <- topoWorld[["latitude"]]
z <- topoWorld[["z"]]
imagep(lon, lat, z, breaks = cm$breaks, col = cm$col)
contour(lon, lat, z, levels = 0, add = TRUE)

center

Resources

To leave a comment for the author, please follow the link and comment on their blog: Dan Kelley Blog/R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)