我在用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中的美学,但是没有任何效果!有什么办法可以解决这个问题吗?
有什么办法可以解决这个问题吗?
在没有数据的情况下,这很难复制,但这里有一个玩具的例子,可以做一些类似你想做的事情。
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()
.