Search code examples
rggplot2colors

Plotting different continous color scales on for multiple y's


I have a scatterplot with two different y's, and I'd like to plot two different continous color scales.

I've tried:

ggplot(cdep_ex2, aes(x = Value)) +
  geom_point(aes(y = ssp1, colour = ssp1))+
  geom_point(aes(y = ssp3, colour = ssp3))+
  theme_minimal()

but this only generates one color scale: enter image description here

Example data:

structure(list(name = c("Afghanistan", "Albania", "Algeria", 
"Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", 
"Austria", "Azerbaijan", "Bahamas", "Bangladesh", "Barbados", 
"Belarus", "Belgium", "Belize", "Benin", "Bosnia and Herzegovina", 
"Botswana", "Brazil"), Value = c(38.1, 38.5, 73.8, 42.9, 97.5, 
-139, 73.5, -132.6, 17.8, 31.5, 99, 11.2, 100, 9.4, 65.1, 21.4, 
42.2, 25, 81.6, -28.3), ssp1 = c(-0.023285480432827, -0.0195475791739413, 
-0.0152821547742994, -0.0297665145694667, -0.00112, 0.0034208655209618, 
-0.0233080809874963, -0.0119891872925497, -0.0348061823905219, 
-0.0246969874800761, -0.0219115916677502, -0.0171838327324434, 
-0.0114035555458779, -0.0137934124932153, -0.0194410708745407, 
-0.0125498418642347, -0.0139770688256968, -0.0248408307973751, 
-0.0349822475252893, 0.00403185973006026), ssp3 = c(0.0165408262675261, 
0.0421500006622914, 0.0294600077546995, 0.0132054069859517, 0.01446, 
0.0978183956088788, 0.0402932291192641, 0.0739753517689263, 0.0359392632125568, 
0.0281158410786377, 0.115765068529543, 0.0592543297697314, 0.143644568572087, 
0.0692833712300024, 0.0471051534387597, 0.137596874025331, 0.0177858814701841, 
0.0422263103089619, -0.0122921113893916, 0.0243272079578788)), row.names = c(NA, 
20L), class = "data.frame")

Solution

  • Use ggnewscale:

    ggplot(cdep_ex2, aes(x = Value)) +
      geom_point(aes(y = ssp1, colour = ssp1)) +
      ggnewscale::new_scale_color() +
      geom_point(aes(y = ssp3, colour = ssp3)) +
      scale_color_gradient(low="#ff4444", high="#ffaaaa") +
      theme_minimal()
    

    enter image description here