1

I'm having trouble overlaying a map (from map package) with raster data (from ggplot2 geom_tile)?

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

Here's my data

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

Here's the map

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) 

I'm not able to combine my raster data with the map..

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

Thank you for any insights! I'm open to using geom_raster or geom_tile.

James
  • 123
  • 1
  • 8

1 Answers1

0

When you make a ggplot and save it to an object as so:

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

and want to add more layers which will be saved to the same object you need to explicitly state so

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

to add a geom_tile layer:

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

another key concept is to specify inherit.aes = F in layers using specific data, if you defined the aes in the ggplot call. Since in most cases it wont be applicable to layers with specific data:

result:

enter image description here

To match the legend one could do

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

enter image description here

resulting in the legend not looking continuous. A way to fix this is to specify geom_path instead of geom_polygon and avoid the alpha fuss altogether:

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

enter image description here

missuse
  • 19,056
  • 3
  • 25
  • 47
  • Thank you!! Is it possible to also shade the legend so that the colors in the plot and legend match? – James Nov 03 '17 at 12:31
  • @James Glad to help. To match the alpha do `+guides(fill = guide_legend(override.aes = list(alpha = 0.5)))`. this will make a slightly different legend. Will update answer – missuse Nov 03 '17 at 12:38