如何添加更多颜色以在 R 中绘图
How to add more colors to plot in R
我有这样的数据集
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 种颜色。如何指定要使用的更多颜色?我想要这样的情节:
您可以使用numCats
参数来设置类别数。 RColorBrewer
调色板有 9 种颜色,但 rworldmap
会在它们之间进行插值以给出您要求的数字。
您还可以试验 catMethod
参数,它决定了分类的完成方式,并且可以从根本上改变地图的外观,如 here 所述。
mapCountryData(sPDF, numCats=20, catMethod="fixedWidth", colourPalette=brewer.pal(9, "RdPu"))
我有这样的数据集
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))
这就是我得到的情节
您可以使用numCats
参数来设置类别数。 RColorBrewer
调色板有 9 种颜色,但 rworldmap
会在它们之间进行插值以给出您要求的数字。
您还可以试验 catMethod
参数,它决定了分类的完成方式,并且可以从根本上改变地图的外观,如 here 所述。
mapCountryData(sPDF, numCats=20, catMethod="fixedWidth", colourPalette=brewer.pal(9, "RdPu"))