R

时间:2017-10-27 12:52:23

标签: r ggplot2

我已经看过现有的踏板,但我无法纠正我的代码。我必须划分传奇"分段"分为两个不同的传说。一个传奇应该是显示(Run,Walk),其他传奇应该告诉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

enter image description here

1 个答案:

答案 0 :(得分:2)

问题是你已经为同一个x,y位置的数据调用了geom_point()两次,并为Staypoint和Segmentation分配了相同的美学(填充和形状),所以ggplot将它们放在同一个图例中。如果为一个变量指定fill =,为另一个变量指定shape =,则应该使用不同的图例。此外,并非所有点都具有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))