Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 调整ggplot2中的图例.title、图例.text和图例颜色_R_Ggplot2 - Fatal编程技术网

R 调整ggplot2中的图例.title、图例.text和图例颜色

R 调整ggplot2中的图例.title、图例.text和图例颜色,r,ggplot2,R,Ggplot2,我无法更改绘图中分段图例的颜色。我需要两种不同的颜色,它作为一个文本在图例中,以及在视觉情节 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,

我无法更改绘图中分段图例的颜色。我需要两种不同的颜色,它作为一个文本在图例中,以及在视觉情节

   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")

er像下面这样的东西应该可以工作。
只需明确指定图例标题,并在字符串末尾添加
\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")

最简单的方法可能是添加
+scale\u color\u continuous(name=“Speed(m/s)\n”)
谢谢,但我遇到了错误scale\u color\u continuous(name=“Speed(m/s)\n”)在+scale\u color\u continuous(name=“Speed(m/s)\n”)中出错:一元运算符的参数无效,奇怪。您确定代码中的其他地方没有出现错误吗?例如,当您省略
+比例\u颜色\u conti…
时是否会出现错误?顺便说一句,您应该根据答案更改原始问题的代码/输入。否则,人们将无法理解这个问题本应解决的问题,即说“你不应该……”:-)Saara great!我还添加了第二个选项,这可能会有所帮助。您知道如何更改分段图例颜色吗?但我应该在哪里添加此选项?只需将代码第2行中的
fill=segmentation
替换为
col=segmentation
。将步行/跑步按点可视化也没有意义,因为您在整个分段中都这样做,使用
geom_线(aes(col=SpeedMeterPerSecond),lty=Segmentation)可能更有意义