Colours of the legend items in ggplot2 not linked to my dataset

110 Views Asked by At

As per title, my problem in ggplot2 is that the colours of the legend are not linked to the ones displayed on the map.

I have a variable which is a map of Europe:

# Create a map of Europe
eu_base <- qmap(location = "Cologne, Germany", zoom = 4, color = "bw", extent = "panel") 

Then I have a dataframe with 3 columns: lat, long, and value (duration) called TimeOverdf. I then create discrete values of the column value within the TimeOverdf variable:

# I create a variable with the breaks
brk<-unique(c(1,seq(10,max(TimeOverdf$value),10),max(TimeOverdf$value)))

# Create a column on my dataframe with discrete values
TimeOverdf$value_discrete <- cut(TimeOverdf$value, breaks=brk, include.lowest=T)

when I then create my plot with discrete values and colours:

eu_base +
  geom_point(data = TimeOverdf, aes(x = lon, y = lat, colour = value_discrete),
             alpha = 0.01) +
  scale_colour_manual("Duration",
                      values = colorRampPalette(c("blue", "yellow", "red"))(length(brk)-1), 
                      breaks=waiver())

The colours of the legend items are all grey and seem not linked to the colours I specify in scale_colour_manual. I have the impression they are linked to the eu_base map. How can I get the colors in the legend to be consistent with those on the plot?

1

There are 1 best solutions below

1
On BEST ANSWER

Add guides(color = guide_legend(override.aes = list(alpha = 1)))