Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dataframe 用选定的有色国家绘制世界地图_Dataframe_Ggplot2_Mapping_Coordinates_Ggmap - Fatal编程技术网

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

Dataframe 用选定的有色国家绘制世界地图,dataframe,ggplot2,mapping,coordinates,ggmap,Dataframe,Ggplot2,Mapping,Coordinates,Ggmap,我试图根据我的数据绘制世界地图,只突出确定的国家。 我试图实现的是在蓝色阴影中为负值着色(数字越大,颜色越深,例如,-5表示深蓝色,-1表示浅蓝色),在红色阴影中为正值(5表示深,1表示浅红色)。 另外,我想给相应的标签(“-5”、“-4”、“-3”…“4”、“5”);我只对将值分为11组(从-5到+5)感兴趣,这意味着一个值为97的国家的颜色将与值为5的国家的颜色相同。 这是我的密码: library(RColorBrewer) library(maptools) library(ggplot

我试图根据我的数据绘制世界地图,只突出确定的国家。 我试图实现的是在蓝色阴影中为负值着色(数字越大,颜色越深,例如,-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, map=wrld, aes(map_id=country, fill=brk),  color="#7f7f7f", 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", colour = NA),
                   panel.grid = element_blank(),
                   axis.text = element_blank(),
                   axis.ticks = element_blank(),
                   legend.position = "bottom")
  gg

}

plotme()
库(RColorBrewer)
图书馆(地图工具)
图书馆(GG2)
如果(!require(gpclib))安装.packages(“gpclib”,type=“source”)
gpclibPermit()
数据(wrld_siml)
ddf=读取。表格(文本=”
国家价值
“阿根廷”2
“澳大利亚”3
“比利时”5
“巴西”17
“加拿大”2
‘中国’-2
“法国”7
‘德国’97
“印度尼西亚”-2
“意大利”9
"日本"9
“葡萄牙”-2
"俄罗斯"3
“沙特阿拉伯”2
新加坡-5
“斯洛文尼亚”1
“西班牙”-3
“瑞士”0
“土耳其”0
'美国'18',页眉=真)

plotme用
nlevels(ddf$brk)
替换标尺中的
length(ddf$value)
是否有帮助?很遗憾,没有。。但是谢谢你的努力!