合并 coord_proj 和 geom_raster

Combine coord_proj and geom_raster

我正在寻找一种在小比例尺地图上绘制栅格数据(使用 ggplotgeom_raster)的方法。我想在 shapefile 的特定区域使用 ggaltcoord_proj 到 'zoom-in',但我 运行 进入错误 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))

是否有另一种使用 coord_proj 生成放大栅格地图并避免 geom_raster 仅适用于笛卡尔坐标的限制的简单方法?

我能想到的其他选项是为每个 'zoomed-in' 区域生成单独的 shapefile,但我正在绘制许多这样的栅格,并且不想为每个栅格生成单独的 shapefile,而是使用 coord_proj 以编程方式指定栅格地图限制。

谢谢

我认为您需要使用 geom_tile() 而不是 geom_raster()geom_raster()内部使用了一个rasterGrob,这是一个只能线性缩放的位图。因此,笛卡尔坐标系受到限制。 geom_tile() 绘制可以转换为任何坐标系的单个矩形。

我没有你的数据集,但我可以举一个非常简单的例子:

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

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