Base R Choropleth:未根据 interval/breaks 的顺序将颜色应用于地图,这使得地图难以阅读

Base R Choropleth: colors aren't being applied to the map according to the order of the interval/breaks which makes the map hard to read

我创建了一个以 R 为基数的等值线,但我在颜色上遇到了困难。首先,颜色与间隔的顺序不同,其次,两个间隔使用相同的颜色,所有这些都使图形难以阅读。无论我使用多少种颜色,都会发生这种情况。我使用的是 brewer.pal 还是基色也无关紧要。Here 是一张地图,其各自的图例说明了这个问题。

以下是我在下载数据后用于创建图表的语句:

#相关包:

library(dplyr)
    library(RColorBrewer)
    library(rgdal)

#创建颜色向量

pop_colors <- brewer.pal(8,"Purples")

#create breaks/intervals

pop_breaks <- c(0,20000,40000,60000,80000,100000,120000)

#apply breaks to population

cuts <- cut(cal_pop$Pop2016, pop_breaks, dig.lab = 6)

#根据人口所属的区间创建一个带有颜色的向量:

color_breaks <- pop_colors[findInterval(cal_pop$Pop2016,vec = pop_breaks)]

创建等值线

plot(cal_pop,col = color_breaks, main = "Calgary Population (2016)")

#创建图例

legend("topleft", fill = color_breaks, legend = levels(cuts), title = "Population")

我使用 readOGR() 命令读取形状文件,我正在链接它 here 以防有人有兴趣查看数据。

如果您能给我任何建议,我将不胜感激。 谢谢!

你的错误在这一行:

color_breaks <- pop_colors[findInterval(cal_pop$Pop2016,vec = pop_breaks)]

我无法读取您的数据文件,所以我将使用 sf 包中的内置文件。

library(sf)
nc <- readOGR(system.file("shapes/", package="maptools"), "sids")
str(nc@data)

colors <- brewer.pal(8,"Purples")

#create breaks/intervals    
sid_breaks <- c(0,2,4,6,8,10,12,20,60)

#apply breaks to population    
sid_cuts <- cut(nc$SID79, sid_breaks, dig.lab = 6, include=TRUE)

#create a vector with colors by population according to the interval they belong to:
sid_colors <- colors[sid_cuts]

#Create choropleth    
par(mar=c(0,0,0,0))
plot(nc, col = sid_colors)
legend("bottomleft", fill = colors, legend = levels(sid_cuts), nc=2, title = "SID (1979)", bty="n")