I have a question. I need to create a color palette that I can use in ggplot2. I'm going to explain my problem. My idea is to create a sequential color palette to use for maps. Ideally, it should have many colors around 30 at least. This is because I need to represent a very long scale. Here are some color palettes that I saw on this page that might be similar to what I want and a new palette that would be ideal, or something similar.
Once this color palette is created, I need to use it in ggplot. That is specifically in scale_fill_distiller(palette="new palette created"). Previously I was using the "YlOrBr" palette, but it has few colors, I would need something similar to what I showed. If the palette was sequential, but also included colors like red, orange, yellow, green, light blue, in that order, and about 6 types of these, then having about 36 would be perfect.
In the following image you can see how the graph looks when I use the "YlOrBr" scale, due to the lack of colors, many details of the map are lost.
And the ideal would be to see something like this (the following image was created by copying and pasting with different scales, something very manual just to interpret the idea).
The code that would be generating these images would be the following:
ggplot(f,mapping=aes(x=lon,y=lat))
geom_contour_fill(aes(z=datos_variable,fill=stat(level)))
geom_contour(aes(z=datos_variable),color="black",size=0.2)
scale_fill_distiller(palette ="YlGnBu" ,direction = 1,super=ScaleDiscretised)
mi_mapa
coord_quickmap(xlim = range(f$lon),ylim=range(f$lat),expand = FALSE)
facet_wrap(~nombre_nivel,scales="free", ncol =length(nivel))
labs(x="Longitud",y="Latitud",fill=nombre_escala,title = titulo_grafico)
and f is a dataframe similar to this, where the plots are made with facet_wrap() varying in the level tag. enter image description here
CodePudding user response:
When I need to use a custom palette, I either make the palette one-off because it is a few colors that I know I want, or a custom palette function, or the nifty coloRampPalette
, which "fills in" colors if you provide some that you want in the palette. If you want to just select values from a sequential palette, you can select [1:n] of that palette, all using scale_*_manual.
library(ggplot2)
# palette function (hue palette, s is the starting hue)
hue_pal <- function(n,s=15) {
hues = seq(s, s 360, length = n 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
# ramped palette starting with custom colors
jet_colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
# pre-made palette
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
df <- data.frame(x=runif(10),y=runif(10),what = rep(letters[1:5],each=2))
num_colors <- length(unique(df$what))
ggplot(df, aes(x=x,y=y,color=what))
geom_point(size=5)
scale_color_manual(values = hue_pal(n=num_colors, s=0))
ggplot(df, aes(x=x,y=y,color=what))
geom_point(size=5)
scale_color_manual(values = jet_colors(num_colors))
ggplot(df, aes(x=x,y=y,color=what))
geom_point(size=5)
scale_color_manual(values = cbPalette[1:num_colors])