Reputation: 355
I am looking for a cyclic colormap in R to visualize the wind direction in a contour plot. Google didn't really help and I get the feeling there are no cyclic colormaps in the famous packages like RcolorBrewer
. I can only find results for matlab and python.
Is there an already available cyclic palette to be found somewhere? Or does anyone know how to create it within R?
Upvotes: 3
Views: 1200
Reputation: 1222
My CMasher package contains a large collection of scientific colormaps, including cyclic ones. Despite being written for Python, the colormaps it provides can very easily be used in R, which is detailed here in its online documentation. CMasher's policy also states that if you are not satisfied with any of the colormaps it provides, you can request for me to create one for you that you like.
Upvotes: 1
Reputation: 285
The packages cetcolor
and pals
contain perceptually uniform cyclic colormaps by Peter Kovesi, cf. https://colorcet.com/gallery.html
e.g. after installing the pals
package:
pals:::pal.bands(pals::kovesi.cyclic_mygbm_30_95_c78_s25,
pals::kovesi.cyclic_mrybm_35_75_c68_s25
)
Upvotes: 3
Reputation: 173803
Here's a worked example using the rainbow
function. The data is such that each pixel is coloured according to its bearing in degrees relative to the origin, with its intensity proportional to its distance.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.6.3
df <- data.frame(x = rep(seq(-1, 1, length.out = 100), each = 100),
y = rep(seq(-1, 1, length.out = 100), 100))
df$bearing <- (90 - 180/pi * ((atan(df$y/df$x) %% pi) + pi * (df$y < 0))) %% 360
df$intensity <- sqrt(df$x^2 + df$y^2)
ggplot(df, aes(x, y, fill = bearing, alpha = intensity)) +
geom_raster() +
scale_fill_gradientn(colors = c(rainbow(7), "red")) +
scale_alpha_identity() +
theme_classic() +
coord_equal()
Created on 2020-08-31 by the reprex package (v0.3.0)
Upvotes: 0
Reputation: 851
I think you'd just want to generate a gradient palette which eventually returns to it's first color. So 4 colors, but 5 levels (yellow, blue, green, red, yellow). and then you'd place their locations to be (0, 90, 180, 270, 360)/360.
you can do this with something like:
scale_color_gradientn(colours = c("yellow", "blue", "red", "green", "yellow"), values = c(0, 90, 180, 270, 360)/360)
Upvotes: 1