Question

How can I sort the legend order in a ggmap? I have the following code:

mymap <- ggmap(map) + geom_point(data = mypoints, aes(x =lon, y= lat,colour = month), alpha=0.5, size=5)

I would like the months to appear in order (i.e. Jan, Feb, Mar, Apr... etc.)

Was it helpful?

Solution

Edit

As stated by Tyler Rinker, one way is to use the function factor to order factor levels (in this case, months).
I created some data to use with ggplot, but you can adapt to your data and use the logic with ggmap.

library(ggplot)

x = c(6.2, 2.3, 0, 1.54, 2.17, 6.11, 0.3,
  1.39, 5.14, 12.52, 12.57, 7.13, 13.71)

y = c(7.89, 7.63, 5.29, 8.38, 8.37, 10.5, 21.5,
  16.65, 23.76, 1.77, 1.8, 10.49, 14.01)

month = month.abb  # system constant in correct sort order.

mypoints = data.frame(cbind(x,y,month))

mypoints$month = factor(mypoints$month, 
                       levels=month.abb ) 

ggplot(data = mypoints,aes(x,y)) +
  geom_point(aes(color=month), alpha=0.5, size=5)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top