我正在使用以下R代码,加载
maps
和RColorBrewer
库,创建一个按人口排名颜色编码的世界地图。如下图所示,我使用绿色调色板,较深的绿色表示人口较多。
我想添加一个连续的颜色图例来显示完整的调色板,以表示浅绿色=小人口,深绿色=大人口,但我无法通过maps
找到实现方法。请告诉我在我的地图上添加连续的颜色图例(或颜色键/颜色比例尺)的最简单方法是什么?
# Load libraries
library(maps)
library(RColorBrewer)
# Load world data
data(world.cities)
# Calculate world population by country
world.pop = aggregate(x=world.cities$pop, by=list(world.cities$country.etc),
FUN=sum)
world.pop = setNames(world.pop, c('Country', 'Population'))
# Create a color palette
palette = colorRampPalette(brewer.pal(n=9, name='Greens'))(nrow(world.pop))
# Sort the colors in the same order as the countries' populations
palette = palette[rank(-world.pop$Population)]
# Draw a map of the world
map(database='world', fill=T, col=palette, bg='light blue')
plotrix
的?legend
或color.legend
中使用。 - rawr