用选定的有色国家绘制世界地图

问题描述

我正在尝试绘制世界地图,仅​​根据我的数据突出显示确定的国家/地区。 我想要实现的是在蓝色阴影中为负值(数字越大,颜色越深,例如 -5 将是深蓝色,-1 为浅蓝色)和在红色阴影中为正值(5 =深色,1 = 浅红色)。 另外,我想相应地给出标签(“-5”、“-4”、“-3”...“4”、“5”);我只对削减 11 个集群中的值(从 -5 到 +5)感兴趣,这意味着值为 97 的国家与值为 5 的国家的颜色相同。 这是我的代码

library(RColorBrewer)
library(maptools)
library(ggplot2)
if (!require(gpclib)) install.packages("gpclib",type="source")
gpclibPermit()

data(wrld_simpl)

ddf = read.table(text="
                 country value
                 'Argentina' 2
                 'Australia' 3
                 'Belgium' 5
                 'Brazil' 17
                 'Canada' 2
                 'China' -2
                 'France' 7
                 'Germany' 97
                 'Indonesia' -2
                 'Italy' 9
                 'Japan' 9
                 'Portugal' -2
                 'Russia' 3
                 'Saudi arabia' 2
                 'Singapore' -5
                 'Slovenia' 1
                 'Spain' -3
                 'Switzerland' 0
                 'Turkey' 0
                 'United States' 18",header=TRUE)

plotme <- function() {
  
  # align colors to countries
  
  ddf$brk <- cut(ddf$value,breaks= -5:5,include.lowest=TRUE)
  
  # this lets us use the contry name vs 3-letter ISO
  wrld_simpl@data$id <- wrld_simpl@data$NAME
  
  wrld <- fortify(wrld_simpl,region="id")
  wrld <- subset(wrld,id != "Antarctica") # we don't rly need Antarctica
  
  gg <- ggplot()
  
  # setup base map
  gg <- gg + geom_map(data=wrld,map=wrld,aes(map_id=id,x=long,y=lat),fill="white",color="#7f7f7f",size=0.25)
  
  # add our colored regions
  gg <- gg + geom_map(data=ddf,aes(map_id=country,fill=brk),size=0.25)
  
  # this sets the scale and,hence,the legend
  gg <- gg + scale_fill_manual(values=colorRampPalette(brewer.pal(11,'RdBu'))(length(ddf$value)),name="Country")
  
  # this gives us proper coords. mercator proj is default
  gg <- gg + coord_map()
  gg <- gg + labs(x="",y="")
  gg <- gg + theme(plot.background = element_rect(fill = "transparent",colour = NA),panel.border = element_blank(),panel.background = element_rect(fill = "transparent",panel.grid = element_blank(),axis.text = element_blank(),axis.ticks = element_blank(),legend.position = "bottom")
  gg

}

plotme()

我不明白为什么它不使用我选择的调色板(它也应该有蓝色),而且我无法在 cut() 函数中使用“labels =”参数。一些国家(法国、德国、美国)也没有上色! 这是我得到的情节:

enter image description here

非常感谢!!!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)