我有一个GIS栅格数据框,我想使用六边形瓦片来绘制它。使用tile_geom()的标准方法很简单:
ggplot(raster_df, aes(x, y, fill=blabla)) + geom_tile()然而,我真的希望我的栅格点显示为六角形,而不是矩形,因为美学原因。最终结果应该类似于这篇博客文章:http://www.statsblogs.com/2014/09/02/how-to-create-a-hexagonal-bin-plot-in-sas/
我尝试使用geom_hex()而不是geom_tile(),但是因为geom_hex()似乎考虑到了装箱,所以我不知道如何破解它来显示我的数据。我希望每个栅格点对应一个十六进制,也就是说,没有任何仓储在所有!
感谢您的任何建议。
编辑:按照要求,这里是一个样本数据栅格(它实际上非常接近我使用的,因为我想绘制一个世界地图,并根据自定义统计为每个栅格点着色)
library(maptools)
library(raster)
library(ggplot2)
data('wrld_simpl')
raster_df <- as.data.frame(rasterToPoints(rasterize(wrld_simpl, raster(res=5))))
raster_df$blabla <- rnorm(nrow(raster_df))发布于 2014-12-31 04:48:26
看起来这可能是由于在ggplot2中实现了geom_hex。我使用这个包已经有几年了,我的第一个猜测是尝试一下:
ggplot(raster_df, aes(x, y, fill=blabla)) + geom_hex(stat="identity")但这会抛出一个错误:
Error in ggplot2:::hexGrob(x = raster_df$x, y = raster_df$y, fill = raster_df$blabla) :
could not find function "hexcoords"因此,我寻找出现在hexbin包中的函数hexcoords。我显式地加载该包,然后重试:
library(hexbin)
ggplot(raster_df, aes(x, y, fill=blabla)) + geom_hex(stat="identity")这是可行的。结果并不是特别漂亮,所以更直接地使用hexbin包可能更好一些。
看起来geom_hex()并不是为了直接将数据绘制到六边形而设计的,而没有stat = "binhex"的中间步骤,这与许多其他geom_函数不同。
https://stackoverflow.com/questions/27710462
复制相似问题