将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,在这里您可以通过指定bin的数量或bin的特定起点和终点来对连续数据进行bin。

# 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) )

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...