在ggplot2中调整图例标题、图例文本和图例颜色

3

我无法更改绘图中分割图例的颜色。我需要将其作为图例文本和视觉图中的两种不同颜色。

   er<-  ggmap(sq_map2) + 
   geom_point(data = sisquoc, size = 3,  aes(fill = Segmentation)) +
   geom_line(data = sisquoc, size = 3,  aes(color =SpeedMeterPerSecond)) +
   geom_text(data = sisquoc, aes(label = paste("  ", 
   as.character(Location_ids), sep="")), 
         angle = 60, hjust = 0, color = "sienna4",size = 6 ) 


   gg<- er  +   labs(x ="Longitude", y = "Latitude") +
   theme(axis.title = element_text(size=20), 
     panel.background = element_rect(fill = "white",size = 0.5, linetype = 
   "dotted"),
     panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour 
   = "black"), 
     panel.grid.minor = element_line(size = 0.5, linetype = 'dotted',colour 
   = "black"),
     panel.border = element_rect(colour = "black", fill=NA, size=0.5),
     axis.text.y   = element_text(size=18),
     axis.text.x   = element_text(size=18))

   gg  + theme(legend.position="right", 
         legend.title = element_text(colour="Black", size=18),
         legend.text = element_text(colour="black", size = 15),
         legend.background = element_rect(fill="grey90",
                                    size=0.5, linetype="solid", 
                                    colour ="black"))  + scale_color_continuous(name="Speed (m/s)\n")

1
{btsdaf} - adibender
谢谢,但是我遇到了错误。+ scale_color_continuous(name="速度(m/s)\n") +scale_color_continuous(name = "速度(m/s)\n") 的操作数无效。 - Saara
{btsdaf} - adibender
顺便提一下,你应该根据答案更改原始问题的代码/输入。否则,人们将无法理解问题本应解决的问题。 - adibender
那是想说“你不应该…” :-) - adibender
1个回答

2

以下类似的代码应该有效。只需明确指定图例标题并在字符串末尾添加\n,这将添加一个额外的空行:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col=Petal.Length))+ 
  geom_point() + scale_color_continuous(name="my scale\n")

或者您可以尝试更改图例方向,但是通常当图例位于底部时最紧凑。

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col=Petal.Length))+ 
  geom_point() + theme(legend.direction = "horizontal", legend.position = "bottom")

1
{btsdaf} - adibender
{btsdaf} - Saara
但是我应该在哪里添加这个? - Saara
1
在您的代码第二行中,将 fill = Segmentation 替换为 col = Segmentation - adibender
1
在可视化步行/奔跑时使用点来展示似乎有些不合理,因为你是在各个段中进行的。因此,使用geom_line(aes(col=SpeedMeterPerSecond), lty = Segmentation)可能更加合理。 - adibender
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接