简体   繁体   中英

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. I've checked many other similar posts and I have read that it must be some problem with 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.

I have one dataframe (AA_BMap) consisting of the number of seats, latitude and longitude values.

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

My idea was for the legend to look something like this:

Model legend

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.
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. 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 . It provides a little safety to encourage you to use columns of the same data frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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