在ggmap中排序图例顺序 [英] Sort legend order in ggmap

查看:101
本文介绍了在ggmap中排序图例顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ggmap中对图例顺序进行排序?我有以下代码:

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)

我希望月份按顺序显示(即1月,2月,3月,4月...等)

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

推荐答案

编辑

如泰勒·林克(Tyler Rinker)所述,一种方法是使用函数factor排序因子水平(在这种情况下为 months ).
我创建了一些要与ggplot一起使用的数据,但是您可以适应您的数据并在ggmap中使用逻辑.

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)

这篇关于在ggmap中排序图例顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆