1

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

Community
  • 1
  • 1
Vedda
  • 7,066
  • 6
  • 42
  • 77

1 Answers1

4

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This is great thank you! I was trying to figure out how to shrink the bin sizes by doing bin = 50 (knowing the default is 30), but it does nothing to the graphic. I'd like to shrink them down more so it looks like a gradient instead of boxes, and also get them within the borders of the states. What options should I look at to get this look? Thanks again – Vedda Jan 20 '15 at 19:38
  • The parameter name is `bins=`, not `bin=`. You'll never get tiles to fit perfectly within non-rectangular state borders. If that's what you want, this is not a good strategy. But that's a different question than the one originally posed. – MrFlick Jan 20 '15 at 19:43
  • ok thanks! This worked, but I'm still not sure if this is the correct way to format the map....i'll work on it some more and post another question later. Thanks for your help. – Vedda Jan 20 '15 at 19:46