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
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
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
p_tile + coord_polar()