Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make a ggplot2 graph with the colors set to a gradient except that I want the values to be log transformed?

I’m making a ggplot2 graph in which I’ve mapped an extremely wide range of numbers to the color. The problem is that my numbers are log-normally distributed, so I have a bunch of numbers all getting set to the same color since the gradient in ggplot2 assumes that the values are on a pretty similar linear scale. Here are some example data:

DF <- data.frame(A = 1:10, 
             B = 1:10, 
             C = 10^seq(-5, 4))

When I use the regular ggplot2 settings, all but my very last point are dark blue.

ggplot(DF, aes(x = A, y = B, color = C)) +
   geom_point()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I log transform these values, the colors look better, but now my legend shows log-transformed values rather than the actual values.

ggplot(DF, aes(x = A, y = B, color = log10(C))) +
   geom_point()

enter image description here

I also tried using scale_color_gradientn in hopes that maybe I could set the numerical breaks the way I wanted, but that really did not work. The extremely small values are now all grey or missing.

ggplot(DF, aes(x = A, y = B, color = C)) +
   geom_point() +
   scale_color_gradientn(colors = grDevices::blues9, 
                         values = 10^seq(-5, 4))

enter image description here

Is there a way to make the colors of my gradient be on a log10 scale but the labels still be the actual values?

>Solution :

You were almost there, you need to specify the color range:

library(ggplot2)

DF <- data.frame(A = 1:10, B = 1:10, C = 10^seq(-5, 4))

ggplot(DF, aes(x = A, y = B, color = C)) +
  geom_point(size = 3) +  
  scale_color_gradient(trans = 'log10',  
                       low = "lightblue", high = "darkblue",, 
                       breaks = 10^seq(-5, 4),  
                       labels = scales::comma) +  
  labs(color = "C Value") +  
  theme_minimal()  

which gives

enter image description here

If you still think the light blue colors are too greyish (light blue contains grey, I would advise you to change to other colors:

library(ggplot2)

DF <- data.frame(A = 1:10, B = 1:10, C = 10^seq(-5, 4))

ggplot(DF, aes(x = A, y = B, color = C)) +
  geom_point(size = 3) +  
  scale_color_gradient(trans = 'log10',  
                       low = "blue", high = "red", 
                       breaks = 10^seq(-5, 4),  
                       labels = scales::comma) +  
  labs(color = "C Value") +  
  theme_minimal()  

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading