在将 sf 对象传递给 ggmap R 时指定数字箱



我有一个数据帧对象,它是通过使用 sf::read_sf 读取形状文件创建的,并与一些预先存在的数据合并到一个公共地理列中:

boundaries <- sf::read_sf('./shapefile')
map <- merge(boundaries, data, by.x = "InterZone", 
by.y = "IntermediateZone2011Code", all.x = FALSE, duplicateGeoms = TRUE)

然后使用 ggmap 将其覆盖在通过 sf get_map 函数获得的提供程序磁贴的顶部:

myMap <- get_map(location =  c(lon = -2.27, lat = 57.1), zoom = 6,
maptype="toner", crop=FALSE, source = 'stamen')
ggmap(myMap) +
geom_sf(data = map, aes(fill=as.factor(column1)), inherit.aes = FALSE) +
scale_fill_brewer(palette = "OrRd") +
coord_sf(crs = st_crs(4326)) + 
labs(x = 'Longitude', y = 'Latitude', fill = 'column1') + 
ggtitle('column1') 

问题是此自动会创建数百个箱。

我一直在浏览文档,但找不到其他参数来指定箱的数量。我怎样才能清楚地按固定数量的箱细分列,然后映射它?

如果没有可重现的示例,很难确切地说出发生了什么,但看起来您可能会将连续变量转换为具有fill=as.factor(column1)的因子。

一种选择是删除as.factor并使用您选择的scale_fill_continuous或其他一些连续色阶。

另一种选择是查看cut,您可以通过指定箱的数量或箱的特定起点和终点来装箱连续数据。

# Make n bins
map$data_bin <- cut(map$column, breaks = n )
# Or make specific start and end points for bins
map$data_bin <- cut(map$column, breaks = c(-Inf,50,100,Inf) )

最新更新