Vedda
Vedda

Reputation: 7445

Cleaning up a map using geom_tile

Thanks to help from some users on this site, I was able to get a nice map plot for some data using geom_point. (Get boundaries to come through on states) However, now I'm trying to clean it up as I have more years to plot and want to make sure the plot is working and providing good information. After some further research, it seems the geom_tile would actually be better for this as it would shy away from points and use a gradient.

The problem I'm running into is getting the code to work with geom_tile. It isn't plot anything and I'm not sure why.

Here's the dataset :

https://www.dropbox.com/s/0evuvrlm49ab9up/PRISM_1895_db.csv?dl=0

Here's the original code with geom_points :

PRISM_1895_db <- read.csv("/.../PRISM_1895_db.csv")

regions<- c("north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas", "illinois", "indiana", "wisconsin")
ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
  geom_point(data = PRISM_1895_db, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) +
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)

enter image description here

And here is the code I've been trying, but none of the data is showing up.

ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
  geom_tile(data = PRISM_1895_db, aes(x = longitude, y = latitude, fill = APPT), alpha = 0.5, color = NA)
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)

enter image description here

Upvotes: 1

Views: 1247

Answers (1)

MrFlick
MrFlick

Reputation: 206616

geom_tile needs your x and y values to be sampled on an regular grid. It needs to be able to tile the surface in rectangles. So your data is irregularly sampled, it's not possible to divide up the raw data into a bunch of nice tiles.

One option is to use the stat_summary2d layer to divide your data into boxes and calculate the average APPT for all points in that box. This will allow you to create regular tiles. For example

ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
  stat_summary2d(data=PRISM_1895_db, aes(x = longitude, y = latitude, z = APPT)) +
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)

which produces

enter image description here

you can look at other options to control this bin sizes if you like. But as you can see it's "smoothing" out the data by taking averages inside bins.

Upvotes: 4

Related Questions