3

I am looking for a means of plotting raster data (using ggplot and geom_raster) on a small scale map. I would like to use ggalt and coord_proj to 'zoom-in' on particular regions of the shapefile, but I run into the error geom_raster only works with Cartesian coordinates

ggplot() +
    geom_polygon(data = land_df, aes(long, lat, group = group), fill = 'grey25')+
    geom_raster(data = df, aes(lon_bin, lat_bin, fill = sum_hours)) +
    coord_proj(xlim = c(-67, -63),ylim = c(0, 9))

Is there another simple means of generating zoomed in raster maps using coord_proj and avoiding this restriction that geom_raster only works with Cartesian coordinates?

The other options I can think of is to generate individual shapefiles for each 'zoomed-in' region, but I am plotting many of these rasters and would rather not have to generate individual shapefiles for each and instead use coord_proj to programmatically specify raster map limits.

Thanks

Nate Miller
  • 386
  • 5
  • 19
  • can you add some data to help make this reproducible? (`coord_proj()` author here). this sounds interesting. – hrbrmstr Jan 09 '18 at 18:36
  • @hrbrmstr I think he needs to use `geom_tile()` instead of `geom_raster()`, and there's nothing you can do in `coord_proj()` to fix it. See my posted answer. – Claus Wilke Jan 10 '18 at 04:45
  • @hrbrmstr as Claus states, it sounds like it may be a inherent, but in this particular case unfortunate, feature of geom_raster. – Nate Miller Jan 10 '18 at 16:35
  • @hrbrmstr One quick question. Given that geom_raster requires cartesian coordinates, is it possible to use coord_proj to specify the x, y bounds to a map, but do so in cartesian coordinates? Given I am doing country level maps, I may be able to get away with Cartesian coords rather than a projection like Robinson – Nate Miller Jan 11 '18 at 04:07

1 Answers1

7

I think you need to use geom_tile() instead of geom_raster(). geom_raster() internally uses a rasterGrob, which is a bitmap that can only be scaled linearly. Hence the limitation to Cartesian coordinate systems. geom_tile() draws individual rectangles which can be transformed into any coordinate system.

I don't have your dataset, but I can show a very simple example:

df <- data.frame(x = 1:100) # a very simple dataset

p_raster <- ggplot(df, aes(x, fill = x, y = 0)) + 
  geom_raster() + 
  scale_fill_distiller()

p_raster

enter image description here

p_raster + coord_polar()
## Error: geom_raster only works with Cartesian coordinates

Now with geom_tile():

# for geom_tile(), map both fill and color to avoid drawing artifacts
p_tile <- ggplot(df, aes(x, color = x, fill = x, y = 0)) + 
  geom_tile() + 
  scale_fill_distiller() +
  scale_color_distiller()

p_tile

enter image description here

p_tile + coord_polar()

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Thank you Claus. I have avoided using geom_tile because it is considerable slower to render the plot, (likely because it is drawing individual tiles) and because the end product looks tiled (even when setting the tile color and fill the same ) rather than a smooth surface. For glossy presentation quality images is not going to be sufficient. However, as you say, it looks like its the only quick option. I'll likely have to pursue a more labor intensive method if I wish to stay with the raster. – Nate Miller Jan 10 '18 at 16:37
  • If `geom_tile()` creates a tiled-looking result, you can play around with the line size and see if this helps. The default setting is 0.1. Making it larger or smaller may help. – Claus Wilke Jan 10 '18 at 17:23