【问题标题】:Sort legend order in ggmap在ggmap中排序图例顺序
【发布时间】:2013-10-22 01:27:52
【问题描述】:

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

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

我希望月份按顺序显示(即一月、二月、三月、四月...等)

【问题讨论】:

标签: r legend ggmap


【解决方案1】:

编辑

正如 Tyler Rinker 所说,一种方法是使用函数 factor 对因子水平进行排序(在本例中为 )。
我创建了一些用于 ggplot 的数据,但您可以适应您的数据并将逻辑与 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多