在 ggplot/ggmap 中绘制多个数据框并创建统一图例的问题

Posted

技术标签:

【中文标题】在 ggplot/ggmap 中绘制多个数据框并创建统一图例的问题【英文标题】:Issues with plotting multiple data frames in ggplot/ggmap & creating a unified legend 【发布时间】:2020-11-19 07:36:16 【问题描述】:
library(sp)
library(sf) 
library(ggplot2) 
library(ggmap) 

格式化纽约地区的边界框

region.bb = c(left=-75,bottom=40,right=-72.5,top=42)
metro.bb = c(left=-74.23,bottom=40.58,right=-73.7,top=41)
nyc.stamen <- get_stamenmap(bbox=metro.bb,zoom=10,maptype="terrain-background")

为不同类别的点格式化单独的纬度/经度数据帧


##### Mesonet
# Mesonet Latitude & longitude data collected from: http://nysmesonet.org/networks/standard 
meso.longitude <- c(-73.964482,-73.953678,-73.893522,-73.815856,-74.148499)
meso.latitude <- c(40.767544,40.631762,40.872481,40.734335,40.604014)
meso.data <- data.frame(meso.longitude,meso.latitude)
rownames(meso.data) <- c("MANH","BKLN","BRON","QUEE","STAT")
##### METER
meter.longitude <- c(-73.950311)
meter.latitude <- c(40.815313)
meter.data <- data.frame(meter.longitude,meter.latitude)
rownames(meter.data) <- c("METER")
##### Wastewater Treatment Plants
water.longitude <- c(-73.9465,-73.9585,-73.9223)
water.latitude <- c(40.7315,40.8217,40.7905)
water.data <- data.frame(water.longitude,water.latitude)
rownames(water.data) <- c("NEWTOWN_CREEK","NORTH_RIVER","WARDS_ISLAND")
##### West Farms Bus Depot
depot.longitude <- c(-73.877744)
depot.latitude <- c(40.837525)
depot.data <- data.frame(depot.longitude,depot.latitude)
rownames(depot.data) <- c("BUS_DEPOT")
##### Hunts Point Produce Market
market.longitude <- c(-73.8796)
market.latitude <- c(40.8105)
market.data <- data.frame(market.longitude,market.latitude)
rownames(market.data) <- c("HUNTS_POINT")
##### Airports
airport.longitude <- c(-73.873983,-73.7781)
airport.latitude <- c(40.776969,40.6413)
airport.data <- data.frame(airport.longitude,airport.latitude)
rownames(airport.data) <- c("LGA","JFK")

使用 ggplot (ggmap) 在雄蕊图上绘制点

mesonet.map <- ggmap(nyc.stamen) +
  xlab("Longitude") +
  ylab("Latitude") +
  geom_point(meso.data,aes(x=meso.longitude,y=meso.latitude,colour='black')) +
  geom_point(meter.data,aes(x=meter.longitude,y=meter.latitude,colour='red')) +
  geom_point(water.data,aes(x=water.longitude,y=water.latitude,colour='blue')) +
  geom_point(depot.data,aes(x=depot.longitude,y=depot.latitude,colour='green')) +
  geom_point(market.data,aes(x=market.longitude,y=market.latitude,colour='pink')) +
  geom_point(airport.data,aes(x=airport.longitude,y=airport.latitude,colour='yellow'))
  # theme(plot.margin=margin(t=5,r=15,b=5,l=5,unit="pt"))
print(mesonet.map)

在此之后,我想创建一个代表每个类别的图例,但我不确定该怎么做。谢谢!

【问题讨论】:

【参考方案1】:

可以这样实现。不是将每个数据框单独绘制成一个数据框,而是将所有数据绘制在一个geom_point 层上,将数据类型映射到颜色上,您会自动获得一个漂亮的图例。要获得正确的颜色,您可以定义一个命名的颜色矢量,该矢量可以使用scale_color_manual 应用于绘图。试试这个:

library(sp)
library(sf) 
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(ggplot2) 
library(ggmap) 
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
library(dplyr) 
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

region.bb = c(left=-75,bottom=40,right=-72.5,top=42)
metro.bb = c(left=-74.23,bottom=40.58,right=-73.7,top=41)
nyc.stamen <- get_stamenmap(bbox=metro.bb,zoom=10,maptype="terrain-background")
#> Source : http://tile.stamen.com/terrain-background/10/300/383.png
#> Source : http://tile.stamen.com/terrain-background/10/301/383.png
#> Source : http://tile.stamen.com/terrain-background/10/302/383.png
#> Source : http://tile.stamen.com/terrain-background/10/300/384.png
#> Source : http://tile.stamen.com/terrain-background/10/301/384.png
#> Source : http://tile.stamen.com/terrain-background/10/302/384.png
#> Source : http://tile.stamen.com/terrain-background/10/300/385.png
#> Source : http://tile.stamen.com/terrain-background/10/301/385.png
#> Source : http://tile.stamen.com/terrain-background/10/302/385.png

##### Mesonet
# Mesonet Latitude & longitude data collected from: http://nysmesonet.org/networks/standard 
meso.longitude <- c(-73.964482,-73.953678,-73.893522,-73.815856,-74.148499)
meso.latitude <- c(40.767544,40.631762,40.872481,40.734335,40.604014)
meso.data <- data.frame(meso.longitude,meso.latitude)
rownames(meso.data) <- c("MANH","BKLN","BRON","QUEE","STAT")
##### METER
meter.longitude <- c(-73.950311)
meter.latitude <- c(40.815313)
meter.data <- data.frame(meter.longitude,meter.latitude)
rownames(meter.data) <- c("METER")
##### Wastewater Treatment Plants
water.longitude <- c(-73.9465,-73.9585,-73.9223)
water.latitude <- c(40.7315,40.8217,40.7905)
water.data <- data.frame(water.longitude,water.latitude)
rownames(water.data) <- c("NEWTOWN_CREEK","NORTH_RIVER","WARDS_ISLAND")
##### West Farms Bus Depot
depot.longitude <- c(-73.877744)
depot.latitude <- c(40.837525)
depot.data <- data.frame(depot.longitude,depot.latitude)
rownames(depot.data) <- c("BUS_DEPOT")
##### Hunts Point Produce Market
market.longitude <- c(-73.8796)
market.latitude <- c(40.8105)
market.data <- data.frame(market.longitude,market.latitude)
rownames(market.data) <- c("HUNTS_POINT")
##### Airports
airport.longitude <- c(-73.873983,-73.7781)
airport.latitude <- c(40.776969,40.6413)
airport.data <- data.frame(airport.longitude,airport.latitude)
rownames(airport.data) <- c("LGA","JFK")

d <- list(meso = meso.data, meter = meter.data, water = water.data,
          depot = depot.data, marker = market.data, airport = airport.data) %>% 
  lapply(function(x) rename_all(x, ~ gsub("\\w+\\.longitude", "longitude", .x)) %>% rename_all(~ gsub("\\w+\\.latitude", "latitude", .x))) %>% 
  bind_rows(.id = "type")

cols <- c(meso = "black", meter = "red", water = "blue", depot = "green", marker = "pink", airport = "yellow")

mesonet.map <- ggmap(nyc.stamen) +
  xlab("Longitude") +
  ylab("Latitude") +
  geom_point(data = d, aes(x = longitude, y = latitude, colour = type)) +
  scale_color_manual(values = cols)
# theme(plot.margin=margin(t=5,r=15,b=5,l=5,unit="pt"))
print(mesonet.map)

【讨论】:

以上是关于在 ggplot/ggmap 中绘制多个数据框并创建统一图例的问题的主要内容,如果未能解决你的问题,请参考以下文章

遍历数据框并绘制每一列

使熊猫具有多索引列的多个数据框并完全连接

循环创建多个数据框并写入excel

如何使用比较多个数据框并使用熊猫返回匹配项

Pandas:有没有办法遍历数据框并使用多个条件创建新的数据框?

Microsoft Access:在插入之前获取当前记录并创建子记录