简体   繁体   English

传说消失

[英]Legend disappears

This is my first post here, but I'll try to provide all the necessary information.这是我在这里的第一篇文章,但我会尽力提供所有必要的信息。

I am using ggplot2 to create a map showing the Domestic PAX volumes by airport for American Airlines in the year 2019. Everything works now with the exception of the legend that, for some reason won't show up.我正在使用 ggplot2 创建一个 map,显示美国航空公司在 2019 年按机场划分的国内 PAX 量。现在一切正常,除了传说,由于某种原因不会出现。 I've checked many other similar posts and I have read that it must be some problem with aes .我检查了许多其他类似的帖子,并且我读到aes一定有问题。 I have noticed that the legends disappear after applying the scale_size_continuous, scale_alpha_continuous and scale_color_viridis, but I have not been able to solve it.我注意到应用 scale_size_continuous、scale_alpha_continuous 和 scale_color_viridis 后图例消失了,但我一直无法解决。

I have one dataframe (AA_BMap) consisting of the number of seats, latitude and longitude values.我有一个 dataframe (AA_BMap) 由座位数、纬度和经度值组成。

Sample:样本:

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

Here is my code:这是我的代码:

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

I am adding an image of what I have so far.我正在添加到目前为止我所拥有的图像。 It is great, it's only missing the legend.太好了,就是少了传说。

Domestic PAX Distribution of AA by Airport in 2019 2019年AA分机场国内PAX分布

My idea was for the legend to look something like this:我的想法是让图例看起来像这样:

Model legend Model 图例

Thank you very much in advance, guys!非常感谢你们,伙计们!

It looks like the problem is that you are supplying breaks that are outside of the range of your data.看起来问题在于您提供的休息时间超出了数据范围。

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

So the highest patch for your legend is 7, while the lowest point in (at least the data sample) is 11.因此,您的图例的最高补丁是 7,而(至少数据样本)中的最低点是 11。
Either removing all of the instances of breaks = mybreaks from your calls to scale_... would work as would setting mybreaks to include items within the range of your Seats data.从您对scale_...的调用中删除所有breaks = mybreaks实例都可以像设置 mybreaks 以包含您的座位数据范围内的项目一样工作。 For example, using your sample data:例如,使用您的示例数据:

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

returns返回带图例的样本

In addition to the changes to mybreaks, ggplot prefers unquoted color = Seats over color = AA_Map$Seats .除了对 mybreaks 的更改之外,ggplot 更喜欢 unquoted color = Seats而不是color = AA_Map$Seats It provides a little safety to encourage you to use columns of the same data frame.它提供了一点安全性来鼓励您使用同一数据框的列。

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

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