1

我无法用栅格数据(来自 ggplot2 geom_tile)覆盖地图(来自地图包)?

library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)

这是我的数据

mydata = read.csv("Seasonal_Temp.csv")
head(mydata)
> head(mydata)
longitude latitude seasonalTavg_Ens
1  -111.688   40.500            3.435
2  -111.688   40.563            3.183
3  -111.688   40.625            3.488
4  -111.625   40.500            3.437
5  -111.625   40.563            3.395
6  -111.625   40.625            3.429

这是地图

states <- map_data("state")
dim(states)
ut_df <- subset(states, region == "utah")
head(ut_df)

counties <- map_data("county")
ut_county <- subset(counties, region == "utah")
head(ut_county)

area <- subset(ut_county, subregion %in% c("summit", "wasatch", "salt lake", 
"utah"))

area_map <- ggplot(data = area, mapping = aes(x = long, y = lat, group = 
group)) + 
coord_fixed(1.3) + 
geom_polygon(color = "black", fill = "white")
area_map + theme_bw()

area_map + coord_fixed(xlim = c(-111.438, -111.688),  ylim = c(40.5, 40.625), 
ratio = 1.3) 

我无法将我的栅格数据与地图结合起来..

ggplot() + geom_raster(data = mydata, aes(longitude,latitude, fill = 
seasonalTavg_Ens)) 

感谢您的任何见解!我愿意使用 geom_raster 或 geom_tile。

4

1 回答 1

0

当您制作 ggplot 并将其保存到对象时:

area_map <- ggplot(data = area, mapping = aes(x = long, y = lat, group = group)) + 
                   coord_fixed(1.3) + 
                   geom_polygon(color = "black", fill = "white")

并且想要添加更多图层,这些图层将保存到您需要明确声明的同一个对象中

  area_map <- area_map + 
              theme_bw() +
              coord_fixed(xlim = c(-111.438, -111.688),
                          ylim = c(40.5, 40.625), atio = 1.3) 

添加 geom_tile 层:

area_map  + geom_raster(data = mydata, aes(longitude, latitude, 
                        fill = seasonalTavg_Ens), inherit.aes = F, alpha = 0.5) 

inherit.aes = F如果您在 ggplot 调用中定义了 aes,则另一个关键概念是使用特定数据在层中指定。因为在大多数情况下,它不适用于具有特定数据的层:

结果:

在此处输入图像描述

为了匹配传说,一个人可以做到

area_map  + geom_raster(data = mydata,
                        aes(longitude,latitude,
                            fill = seasonalTavg_Ens),
                        inherit.aes = F,
                        alpha = 0.5) +
  guides(fill = guide_legend(override.aes = list(alpha = 0.5)))

在此处输入图像描述

导致传说看起来不连续。解决此问题的一种方法是指定 geom_path 而不是 geom_polygon 并完全避免 alpha 大惊小怪:

ggplot() +
  geom_raster(data = mydata,
              aes(longitude,latitude,
                  fill = seasonalTavg_Ens)) +
  geom_path(data = area, aes(x = long, y = lat, group =  group),
               color = "black")+
  coord_fixed(xlim = c(-111.438, -111.688),  ylim = c(40.5, 40.625), 
              ratio = 1.3)+
  theme_bw()

在此处输入图像描述

于 2017-11-03T09:50:30.350 回答