R中ggplot的多个图例 [英] Multiple legends for a ggplot in R

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

问题描述

我已经看到现有的步伐,但我无法纠正我的代码。我必须将传奇分割分成两个不同的传说。应该显示一个传说(Run,Walk)和其他传说应该告诉StayPoint(是,否)。问题在于,所有的图例值都会混淆在同一个图例标题下。任何人都可以告诉我吗?谢谢 !

  ll_meanstt < -  sapply(total_trajectory [1:2],mean)
sq_map2tt< - get_map(location =数据类型(total_trajectory $ tt_lon,total_trajectory $ tt_lat,$($));
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(,
$ b $ 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是
3 5.010716 47.29398 W5234运行1.5否

解决方案

geom_point()在同一个x,y位置的数据两次,并为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))


I have seen existing treads but i couldnt correct my code. I have to divide the legend "Segmentation" into two different legends. One legend should be showing (Run,Walk) and other legend should be telling the StayPoint(Yes, No). Issue is that, all the legend values gets mixed up and comes under the same legend heading. Can anyone tell me about it ? Thank you !

   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

解决方案

The problem is you have called geom_point() twice for data in the same x, y location and assigned the same aesthetics (fill and shape) to both Staypoint and Segmentation, so ggplot is putting them in the same legend. If you specify fill = for one variable, and shape = for the other, they should go into different legends. Also, not all points have fill aesthetics, you need to either select shapes that do(shapes 21 - 25 have fill), or use color =, an aesthetic that all ggplot2 points have.

Example using color instead of fill

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

another approach if you want to use fill instead of 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))

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

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