首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >图例未使用手动定义的颜色显示

图例未使用手动定义的颜色显示
EN

Stack Overflow用户
提问于 2020-05-23 06:10:52
回答 1查看 42关注 0票数 1

我在和ggplot2做地图路线。代码如下:

代码语言:javascript
运行
复制
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中的美学,但都没有工作!

有什么办法解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2020-05-23 10:55:04

这很难在没有数据的情况下复制,但这里有一个玩具示例,它完成了您正在尝试做的事情。

代码语言:javascript
运行
复制
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(..)调用,如下所示:

代码语言:javascript
运行
复制
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)

因此,考虑到这一点,您可以以一种更简单的方式重写代码。

代码语言:javascript
运行
复制
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()分享您的数据结构。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61964625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档