简体   繁体   English

rworldmap颜色比例不连续

[英]rworldmap color scale not continuous

I am using following example data and code to produce a map of world: 我正在使用以下示例数据和代码来生成世界地图:

dF = read.table(text="
country, value
'France',  7
'United Kingdom',  64
'Italy',  68
'Sweden',  82
'Nepal',  85
'Japan',  86
'Germany',  86
'United States',  93
'China',  94
'Nigeria',  95"
, header=TRUE, sep=",")

library(rworldmap)

sPDF <- joinCountryData2Map(dF,
                            joinCode = "NAME",
                            nameJoinColumn = "country",
                            verbose = TRUE)

library(RColorBrewer)
colourPalette <- brewer.pal(7,'RdYlGn')
mapCountryData(sPDF,nameColumnToPlot="value",colourPalette=colourPalette)

However, the colors filled on map are not in a continuous scale. 但是,地图上填充的颜色不是连续的比例。 The United Kingdom is shown in almost the same color as France although their values are very different. 尽管英国的价值差异很大,但与法国的颜色几乎相同。 Also China and United States are filled with different colors although their values are almost same. 尽管中美两国的价值几乎相同,但它们充满了不同的颜色。 I think the colors are being filled as for categorical values and not on a continuous gradient scale. 我认为颜色是按分类值填充的,而不是按连续的渐变比例填充的。 Changing the brewer.pal value from 7 to others does not help. 将brewer.pal值从7更改为其他值无济于事。 How can I correct this? 我该如何纠正? Thanks for your help. 谢谢你的帮助。


Edit 编辑

Adding dput of dF which will help others to reproduce data frame 添加dF dput,这将有助于其他人重现数据帧

structure(list(country = c("France", "United Kingdom", "Italy", 
"Sweden", "Nepal", "Japan", "Germany", "United States", "China", 
"Nigeria"), value = c(7L, 64L, 68L, 82L, 85L, 86L, 86L, 93L, 
94L, 95L)), .Names = c("country", "value"), class = "data.frame", row.names = c(NA, 
-10L))

Your issue is because you are using the default catMethod for mapCountryData which is 'quantiles'. 您的问题是因为您对mapCountryData使用默认的catMethod,即'quantiles'。 If you set catMethod to 'fixedWidth' I think you will get a result closer to what you want. 如果将catMethod设置为“ fixedWidth”,我想您会得到更接近所需结果的结果。

mapCountryData(sPDF,nameColumnToPlot="value",colourPalette=colourPalette,catMethod='fixedWidth')

Or you can specify the category breaks with eg catMethod=c(0,20,40,60,80,100) 或者您可以使用catMethod = c(0,20,40,60,80,100)指定类别中断

colourPalette <- brewer.pal(5,'RdYlGn')
mapCountryData(sPDF,nameColumnToPlot="value",colourPalette=colourPalette,catMethod=c(0,20,40,60,80,100))

Then you can add a more detailed legend if you wish. 然后,您可以根据需要添加更详细的图例。

mapParams <- mapCountryData(sPDF, nameColumnToPlot="value", colourPalette=colourPalette, catMethod=c(0,20,40,60,80,100), addLegend=FALSE)

do.call( addMapLegend, c( mapParams
                          , legendLabels="all"
                          , legendWidth=0.5 ))

Hope that helps. 希望能有所帮助。

Reason behind the plotting was, next value after France is UK plus you have 10 countries and you are dividing them into 7 categories which made France and UK in same color category. 原因是,法国之后的下一个值是英国,加上您有10个国家/地区,并将它们划分为7个类别,这使法国和英国成为同一颜色类别。 By default mapCountryData takes 7 category for plotting... 默认情况下,mapCountryData采用7类进行绘图...

Following chart prepared by your code 按照您的代码准备的图表 按照您的代码准备的图表

Try to provide category exclusively. 尝试仅提供类别。 Try following... 尝试关注...

colourPalette <- brewer.pal(nrow(dF),'RdYlGn')
mapCountryData(sPDF,nameColumnToPlot="value",colourPalette=colourPalette,numCats=nrow(dF))

Following chart prepared after introducing category exclusively 以下是专门介绍类别后准备的图表 以下是专门介绍类别后准备的图表


Update 更新资料

Case where you want only lower values to be colored red (in this case france) 您只希望将较低的值显示为红色的情况(在这种情况下为法国)

library(fields)
colourPalette = two.colors(n=max(dF$value)-min(dF$value)+1, start="red", middle='yellow', end="dark green", alpha=1.0)
colourPalette = colourPalette[dF$value - min(dF$value) + 1]
mapCountryData(sPDF,nameColumnToPlot="value",colourPalette=colourPalette, numCats=nrow(dF))

Output would be 输出将是 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM