我在和ggplot2做地图路线。代码如下:
colombia_map <- ggplot(data = colombia, mapping = aes(x = long, y = lat, group = group)) +
geom_polygon(fill = 'white', color = 'black', size = 1) +
coord_quickmap() +
theme(panel.grid = element_blank(), panel.background = element_blank())
colombia_map1 <- colombia_map +
geom_point(data = nombres[2:3],
aes(x = lon, y = lat),
colour = "Black",
size = 2, inherit.aes = F) +
geom_curve(data = avianca,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Avianca"),
col = "red",
size = 0.5,
curvature = 0.2, inherit.aes = F) +
geom_curve(data = latam,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Latam"),
col = "darkblue",
size = 0.5,
curvature = 0.2, inherit.aes = F) +
geom_curve(data = easy,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Easy Fly"),
col = "darkorange",
size = 0.5,
curvature = 0.2, inherit.aes = F, show.legend = T) +
geom_curve(data = viva,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Viva Air"),
col = "gold",
size = 0.5,
curvature = 0.2, inherit.aes = F, show.legend = T) +
geom_curve(data = satena,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Satena"),
col = "olivedrab",
size = 0.5,
curvature = 0.2, inherit.aes = F, show.legend = T) +
geom_curve(data = wingo,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Wingo"),
col = "purple",
size = 0.5,
curvature = 0.2, inherit.aes = F, show.legend = T) +
geom_text_repel(data = nombres,
aes(x = lon, y = lat, label = Origen),
colour = "black",
size = 3.5, inherit.aes = F) +
scale_color_manual(name = "Operadores",
breaks = c("Avianca", "Latam", "Viva Air", "Easy Fly", "Satena", "Wingo"),
values = c("Avianca" = "red", "Latam" = "darkblue", "Viva Air" = "gold",
"Easy Fly" = "darkorange", "Satena" = "olivedrab",
"wingo" = "purple")) +
theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
rect = element_rect(fill = "transparent"))
这是输出:
问题是,我尝试了所有的方法,我不明白为什么图例没有出现在剧情中。我试着在一般的地图和每个geom_curve中的美学,但都没有工作!
有什么办法解决这个问题吗?
发布于 2020-05-23 02:55:04
这很难在没有数据的情况下复制,但这里有一个玩具示例,它完成了您正在尝试做的事情。
library(ggplot2)
ggplot() +
geom_line(data = economics, aes(x = date, y = psavert, colour = "PSAVERT")) +
geom_line(data = economics, aes(x = date, y = uempmed, colour = "UEMPMED")) +
scale_colour_manual(values = c("PSAVERT"="blue","UEMPMED"="red"))
您必须将颜色值放入aes(...)
中,但您可以在其中显式定义标签,它将直接映射到图例。然后您可以使用scale_colour_manual(values = c(....)
定义您的颜色。但是在您的示例中,您使用显式的颜色调用覆盖了aes(..)
调用,如下所示:
geom_curve(data = latam,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Latam"),
col = "darkblue",
size = 0.5,
curvature = 0.2, inherit.aes = F)
因此,考虑到这一点,您可以以一种更简单的方式重写代码。
colombia_map <- ggplot(data = colombia, mapping = aes(x = long, y = lat, group = group)) +
geom_polygon(fill = 'white', color = 'black', size = 1) +
coord_quickmap() +
theme(panel.grid = element_blank(), panel.background = element_blank())
colombia_map1 <- colombia_map +
geom_point(data = nombres[2:3],
aes(x = lon, y = lat),
colour = "Black",
size = 2, inherit.aes = F) +
geom_curve(data = avianca,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Avianca"),
size = 0.5,
curvature = 0.2) +
geom_curve(data = latam,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Latam"),
size = 0.5,
curvature = 0.2) +
geom_curve(data = easy,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Easy Fly"),
size = 0.5,
curvature = 0.2) +
geom_curve(data = viva,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Viva Air"),
size = 0.5,
curvature = 0.2) +
geom_curve(data = satena,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Satena"),
size = 0.5,
curvature = 0.2) +
geom_curve(data = wingo,
aes(x = flon, y = flat, xend = tlon, yend = tlat, color = "Wingo"),
size = 0.5,
curvature = 0.2) +
geom_text_repel(data = nombres,
aes(x = lon, y = lat, label = Origen),
colour = "black",
size = 3.5, inherit.aes = F) +
scale_color_manual(name = "Operadores",
values = c("Avianca" = "red", "Latam" = "darkblue", "Viva Air" = "gold",
"Easy Fly" = "darkorange", "Satena" = "olivedrab",
"wingo" = "purple")) +
theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
rect = element_rect(fill = "transparent"))
这应该是可行的,但如果没有一个完全可复制的例子,很难保证它。在未来,请使用dput()
分享您的数据结构。
https://stackoverflow.com/questions/61964625
复制相似问题