【问题标题】:Multiple legends for a ggplot in RR中ggplot的多个图例
【发布时间】:2018-04-09 02:06:56
【问题描述】:

我已经看到了现有的踏板,但它无法更正我的代码。我必须将传说“分段”分为两个不同的传说。应该显示一个图例(跑步,步行),而另一个图例应该告诉 StayPoint(是,否)。问题是,所有的图例值都混合在一起,并在同一个图例标题下。谁能告诉我?谢谢 !

   ll_meanstt <- sapply(total_trajectory[1:2], mean)
   sq_map2tt <- get_map(location = ll_meanstt,  maptype = "roadmap", source 
   = "google", zoom = 21)
  sisquoctt <- 
  setNames(data.frame(total_trajectory$tt_lon,total_trajectory$tt_lat, 
  total_trajectory$tt_ids, total_trajectory$Trajectory_Segmentation, 
  total_trajectory$tt_speed, Staypoint), c("lon", "lat", "LocationID", 
  "Segmentation", "SpeedMetersPerSecond", "Staypoint"));



ggmap(sq_map2tt) + 
geom_point(data = sisquoctt, size = 12,  aes(fill = Staypoint, shape = 
Staypoint)) +

geom_point(data = sisquoctt, size = 8,  aes(fill = Segmentation, shape = 
Segmentation)) +

geom_line(data = sisquoctt, size = 3,  aes(color =SpeedMetersPerSecond)) +
geom_label_repel (data = sisquoctt, aes(label = paste("", 

as.character(LocationID), sep="")), 
                  angle = 60, hjust = 2, color = "indianred3",size = 4)

        lon      lat    LocationID Segmentation SpeedMetersPerSecond Staypoint
  1  5.010633 47.29399      W5232         Walk                  1.2        No
  2  5.010643 47.29400      W5769         Walk                  1.0       Yes
  3  5.010716 47.29398      W5234          Run                  1.5        No

【问题讨论】:

  • 请勿以图片形式发布您的数据,请学习如何提供reproducible example
  • 我已经更新了我的问题:)

标签: r ggplot2


【解决方案1】:

问题是您为同一 x、y 位置中的数据调用了两次 geom_point(),并为 Staypoint 和 Segmentation 分配了相同的美学(填充和形状),因此 ggplot 将它们放在同一个图例中。如果您为一个变量指定fill =,为另一个变量指定shape =,它们应该进入不同的图例。此外,并非所有点都具有fill 美学,您需要选择具有fill 的形状(形状21 - 25 具有fill),或使用color =,这是所有ggplot2 点都具有的美学。

使用color 代替fill 的示例

ggmap(sq_map2tt) + 
geom_point(data = sisquoctt, size = 8,  aes(color = Staypoint, shape = Segmentation)) + 
geom_line(data = sisquoctt, size = 3,  aes(color = SpeedMetersPerSecond))

如果你想使用fill 而不是color,另一种方法

ggmap(sq_map2tt) + 
    geom_point(data = sisquoctt, size = 8,  aes(fill = Staypoint, shape = Segmentation)) + 
    scale_shape_manual(values = c(21, 24) +
    geom_line(data = sisquoctt, size = 3,  aes(color = SpeedMetersPerSecond))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多