Sort legend order in ggmap

橙三吉。 提交于 2019-12-02 03:14:35

问题


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.)


回答1:


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)



来源:https://stackoverflow.com/questions/19507545/sort-legend-order-in-ggmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!