0

I am trying to create a continuous color bar in my ggplot using:

scale_color_gradient2(midpoint = 0, mid = "white", 
                      high = muted("blue"), low = muted("red"), 
                      guide = "colourbar", space = "Lab").

My colorbar looks like enter image description here

Does anyone know how to create a

  1. symmetric color bar, that on the left goes down to -80, even tho -80 or -100 or whatever does not exist in the data.

  2. I want to have one color for 0-10, another color for 10-20, 20-30 yet another color. I cannot make any sense of the plots when the color changes gradually.

Lets say my code is

states <- map_data("state")
states_cluster <- subset(states, 
                           region %in% c("washington"))

data %>%
ggplot() +
geom_polygon(data = states_cluster, 
             aes(x = long, y = lat, group = group),
             fill = "grey", color = "black") +
geom_point(aes_string(x = "long", y = "lat", color = "color_num"), 
           alpha = 1, size=.3) +
guides(fill = guide_colourbar(barwidth = .1, barheight = 20))+
scale_color_gradient2(midpoint = 0, mid = "white", 
                     high = muted("blue"), low = muted("red"), 
                     guide = "colourbar", space = "Lab") 

and my data is:

data = data.table(color_num = as.integer(runif(n = 16, min = -10, max = 80)),
                  lat = c(47.78125, 47.78125, 47.78125, 
                          47.78125, 47.78125, 47.78125, 
                          47.78125, 47.78125, 47.78125,
                          47.78125, 47.78125, 47.78125, 
                          47.78125, 47.78125, 47.78125, 
                          47.78125),
                 long = c(-121.21875, -121.28125, -121.34375, 
                           -121.40625, -121.46875, -121.53125,
                           -121.59375, -121.65625, -121.71875, 
                           -121.78125, -121.84375, -121.90625, 
                           -121.96875, -122.03125, -122.09375, 
                           -122.15625))
OverFlow Police
  • 861
  • 6
  • 23
  • I think [this very question and the suggested function](https://stackoverflow.com/q/50506832/7941188) might help you. There is also a link contained to a GitHub thread in which @clauswilke suggests a function for use in `scale`. It's a bit tricky to work with though – tjebo Jul 29 '19 at 20:45
  • Awesome, thanks. I will delete my question since it is duplicate! if it did not cover what I want, I will post a modified version – OverFlow Police Jul 29 '19 at 20:49
  • 1
    OK. just a quick hint regarding your scale - if you chose to use not the 'draw a plot for legend' version, then define the limits in `scale_..._gradient`. But using `scale_...gradientn` comes with another problem - the asymmetric midpoint, if the limits are not symmetrical of course. ([see here](https://stackoverflow.com/q/11299705/7941188)) – tjebo Jul 29 '19 at 20:51
  • 1
    re 1: set `limits = c(-80, 80)` in `scale_color_gradient2`. – Axeman Jul 29 '19 at 20:59

0 Answers0