Home > database >  plot color scale bar with unequal distance between ticks
plot color scale bar with unequal distance between ticks

Time:12-29

How can I plot this legend with ggplot2?(please see the picture) We use geom_point() and scale_color_gradientn(), but the legend is not I want, because the data focused on a point value between 0-5 (all data range between 0-10). I want to show this and get legends of the picture, how can I do this? My code: (but not plot I want legend)

  ggplot(data,aes(x = xx, y = -log10(P), color = -log10(P),size = -log10(P)))   
  geom_point()   
  scale_color_gradientn(colours = c("blue", "yellow","red"),
                  # I set this parameter but not use, the legend ticks labels not equal 
                        breaks = c(0:5,10,15,20), 
                        values = c(0,scales::rescale(c(0:5,10,15,20), 
                                                    from = range(-log10(dt$P))),1))   
  guides(size = "none", color = guide_colorbar(ticks = F)) 

thank you! anyone help me !!!

want to plot legend eg.

CodePudding user response:

For make Pathway Analysis Im use this code, and I put a scale color gradrient.

library(ggplot2)
ggplot(aes(x=`Enrichment Score (-log10(Pvalue))`,y=Term,
                    size=`Count`,  color=`P value`))  
         geom_point(alpha=0.9,stroke = 1) 
         scale_color_gradient2(high = '#4101fa', 
                               mid = '#d8006f', 
                               low = '#f20530', 
                               breaks = c(0.002478,0.0012391 ,0.0001511)) 
         guides(colour = guide_colourbar(reverse = F))

enter image description here

CodePudding user response:

It's not entirely clear to me what you are really intending to get. One reason for this is that you have not provided a minimal, reproducible example. https://stackoverflow.com/help/minimal-reproducible-example

Anyways, I'm trying to guess what you looking for. You need to set breaks in the call to scale_continuous. Also don't forget to set the limits.

library(ggplot2)
ggplot(mtcars, aes(mpg, disp, color = cyl)) 
  geom_point()  
  scale_color_continuous(breaks = c(0, 1, 3, 7, 10), limits = c(0,10))

Created on 2021-12-28 by the reprex package (v2.0.1)

  • Related