3

我有这样的数据集

Country        GDP
Afghanistan    20315
Albania        12800

我的代码如下

library(rworldmap)
library(RColorBrewer)
library(ggplot2)

countries = read.csv("countries.csv", na.strings = " NA")
countries$GDP = as.numeric(as.character(countries$GDP))

sPDF = joinCountryData2Map(countries, joinCode = "NAME", nameJoinColumn =    "Country", verbose = TRUE)
mapParams = mapCountryData(sPDF, nameColumnToPlot = "GDP", missingCountryCol = "dark grey", addLegend = FALSE, oceanCol = "lightsteelblue2")
do.call(addMapLegend, c(mapParams, legendWidth = 0.5, legendMar = 2))

这就是我得到的情节

在此处输入图像描述 如您所见,很多国家都被涂成红色,因为图例中只使用了 7 种颜色。如何指定要使用的更多颜色?我想得到这样的情节:在此处输入图像描述

4

1 回答 1

1

您可以使用numCats参数来设置类别的数量。RColorBrewer调色板有 9 种颜色,但会rworldmap在它们之间进行插值,为您提供所需的数字。

您还可以尝试使用catMethod决定如何进行分类的参数,并可以从根本上改变地图的外观,如此所述。

mapCountryData(sPDF, numCats=20, catMethod="fixedWidth", colourPalette=brewer.pal(9, "RdPu"))

于 2015-05-15T08:53:36.507 回答