繁体   English   中英

传说消失

[英]Legend disappears

这是我在这里的第一篇文章,但我会尽力提供所有必要的信息。

我正在使用 ggplot2 创建一个 map,显示美国航空公司在 2019 年按机场划分的国内 PAX 量。现在一切正常,除了传说,由于某种原因不会出现。 我检查了许多其他类似的帖子,并且我读到aes一定有问题。 我注意到应用 scale_size_continuous、scale_alpha_continuous 和 scale_color_viridis 后图例消失了,但我一直无法解决。

我有一个 dataframe (AA_BMap) 由座位数、纬度和经度值组成。

样本:

structure(list(Airport.Name = c("Grand Rapids", "Grand Rapids", 
"Martha's Vineyard", "Nantucket", "Nantucket", "Gunnison", "Richmond (US)", 
"Richmond (US)", "Duluth", "Kalispell", "Worcester", "Cheyenne Regional Apt", 
"Cheyenne Regional Apt", "Marquette", "Billings", "Billings", 
"Garden City", "Del Rio International Apt", "Springfield (US) IL", 
"Waterloo"), Seats = c(11.5625, 11.5625, 13.984, 16.416, 16.416, 
17.28, 18.493, 18.493, 21.925, 22.724, 24.9, 25.75, 25.75, 26.9075, 
27.664, 27.664, 32.038, 32.226, 34.134, 35), Carrier.Name = c("American 
Airlines", "American Airlines", "American Airlines", "American Airlines", 
"American Airlines", "American Airlines", "American Airlines", 
"American Airlines", "American Airlines", "American Airlines", 
"American Airlines", "American Airlines", "American Airlines", 
"American Airlines", "American Airlines", "American Airlines", 
"American Airlines", "American Airlines", "American Airlines", 
"American Airlines"), Longitude = c("-85.522800000000004", 
"-85.522796630000002", "-70.6143", "-70.060203549999997", 
"-70.060199999999995", "-106.93300000000001", "-77.319699999999997", 
"-77.319702149999998", "-92.193600000000004", "-114.256", 
"-71.875699999999995", "-104.81199650000001", "-104.812", 
"-87.395399999999995", "-108.54299930000001", "-108.54300000000001", 
"-100.724", "-100.92700000000001", "-89.677899999999994", 
"-92.400299070000003"), Latitude = c("42.880800000000001", 
"42.880798339999998", "41.393099999999997", "41.253101350000001", 
"41.253100000000003", "38.533900000000003", "37.505200000000002", 
"37.505199429999998", "46.842100000000002", "48.310499999999998", 
"42.267299999999999", "41.155700680000002", "41.155700000000003", "46.3536", 
"45.807701109999996", "45.807699999999997", "37.927500000000002", 
"29.374199999999998", "39.844099999999997", "42.55709839")), row.names = 
c(NA, 20L), class = "data.frame")

这是我的代码:

library(ggplot2)
library(maps)
library(viridis)
library(mapproj)
'''
USMap <- borders("state", colour = "grey", fill = "white")
mybreaks <- c(0.02, 0.04, 0.08, 1, 7)
'''
AA_BMAP <- ggplot() + theme_void() + USMap +
geom_point(data = AA_BMap, aes(x = as.numeric(Longitude), y = as.numeric(Latitude), color = 
AA_BMap$Seats, size = AA_BMap$Seats, alpha = AA_BMap$Seats), shape=20, stroke=FALSE) +
scale_size_continuous(guide = "legend", name="Seats [thousands]", trans="log", range=c(1,12), 
breaks=mybreaks) +
scale_alpha_continuous(name="Seats [thousands]", trans="log", range=c(0.1, .9), breaks=mybreaks) +
scale_color_viridis(option="magma", trans="log", breaks=mybreaks, name="Seats [thousands]" ) + 
coord_map() +
guides( color = guide_legend()) + 
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(),
 panel.grid.major.x=element_blank(),
 panel.grid.major.y=element_blank(),
 legend.position = c(0.1, 0.8),
 legend.title = element_text(colour="white", size = 16, face='bold'),
 text = element_text(color = "#22211d"),
 plot.background = element_rect(fill = "#f5f5f2", color = NA), 
 panel.background = element_rect(fill = "#f5f5f2", color = NA), 
 legend.background = element_rect(fill = "#f5f5f2", color = NA),
 )

我正在添加到目前为止我所拥有的图像。 太好了,就是少了传说。

2019年AA分机场国内PAX分布

我的想法是让图例看起来像这样:

Model 图例

非常感谢你们,伙计们!

看起来问题在于您提供的休息时间超出了数据范围。

 range(AA_BMap$Seats)
# [1] 11.5625 35.0000
mybreaks <- c(0.02, 0.04, 0.08, 1, 7)

因此,您的图例的最高补丁是 7,而(至少数据样本)中的最低点是 11。
从您对scale_...的调用中删除所有breaks = mybreaks实例都可以像设置 mybreaks 以包含您的座位数据范围内的项目一样工作。 例如,使用您的示例数据:

mybreaks = c( 17, 25, 34)
AA_BMap %>%
      ggplot() + 
      theme_void() + 
      USMap +
      geom_point( aes(x = as.numeric(Longitude), 
                                     y = as.numeric(Latitude), 
                                     color =  Seats, 
                                     size = Seats, 
                                     alpha = Seats),
                 shape=20, 
                 stroke=FALSE
                 ) +
      scale_size_continuous(guide = "legend", 
                            name="Seats [thousands]", 
                            trans="log", 
                            range=c(1,12), 
                            breaks=mybreaks
                            ) +
      scale_alpha_continuous(name="Seats [thousands]",
                             trans="log",
                             range=c(0.1, .9),
                             breaks=mybreaks
                             ) +
      scale_color_viridis(option="magma",
                          trans="log",
                          breaks=mybreaks,
                          name="Seats [thousands]"
                          ) +
      coord_map() +
      guides( color = guide_legend()) +
      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(),
            panel.grid.major.x=element_blank(),
            panel.grid.major.y=element_blank(),
            # legend.position =  c(0.1, 0.8),
            legend.title = element_text(
                  # colour="white", 
                  size = 16, face='bold'),
            text = element_text(color = "#22211d"),
            plot.background = element_rect(fill = "#f5f5f2", color = NA), 
            panel.background = element_rect(fill = "#f5f5f2", color = NA), 
            legend.background = element_rect(fill = "#f5f5f2", color = NA),
      )

返回带图例的样本

除了对 mybreaks 的更改之外,ggplot 更喜欢 unquoted color = Seats而不是color = AA_Map$Seats 它提供了一点安全性来鼓励您使用同一数据框的列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM