gpt4 book ai didi

r - 调整 ggplot2 中的 legend.title、legend.text 和图例颜色

转载 作者:行者123 更新时间:2023-12-01 08:13:14 68 4
gpt4 key购买 nike

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

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

最佳答案

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

关于r - 调整 ggplot2 中的 legend.title、legend.text 和图例颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46931297/

68 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com