create a legend in ggplot2 plot
0
0
Entering edit mode
7.1 years ago
Assa Yeroslaviz ★ 1.6k

Hi,

 

I have a data.frame which I would like to plot using the ggplot2 package.

This is what i am doing:

myBreaks <- c(seq(0,0.001, 0.0002),seq(0.002,0.01,0.005), 0.01, seq(0.2,1,0.3), 2,5)#create the intervals

PlotData$bins <- findInterval(PlotData[,3],myBreaks) # plotting the braks

cols <- c('cadetblue1', 'cadetblue3', 'cadetblue4', 'cadetblue', 'burlywood', 'burlywood2', 'burlywood4', 'brown3', 'darkorchid4', 'darkorange2', 'darkolivegreen3', 'red3', 'midnightblue', 'chocolate2') # wanted colours

ggplot(PlotData, aes(y = start,  x = start.1)) + 
  labs(title = "normalized values") + 
  geom_tile(aes(fill = bins)) + 
  scale_fill_gradientn(name = "values", 
    breaks = myBreaks, 
    colours=cols, 
    labels = format(paste("\u2265", myBreaks,sep="")), 
    values= myBreaks, 
    rescaler = function(x, ...) x, 
    oob = identity, na.value="grey89", 
    guide="legend")

the plot I get shows all the colours in the plot itself but not in the legend. How can I convert the legend to also show all the colours as well as the breaks as  labels?

 

Thanks in advance

Assa

ggplot2 legend • 2.3k views
ADD COMMENT
1
Entering edit mode

Are you sure you posted the correct file? I can't see much in the plot you posted.

ADD REPLY
0
Entering edit mode

Yes I am. The file itself is not important. I dio have the corrct colours there.

 Yes i know there not many elements there, but this expected

i need to have the correct legend

 

ADD REPLY

Login before adding your answer.

Traffic: 2766 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6