如何在R-on大陆级别创建世界热量图
我正在尝试为country.txt文件创建热图,该文件包含以下表格:如何在R-on大陆级别创建世界热量图,r,heatmap,R,Heatmap,我正在尝试为country.txt文件创建热图,该文件包含以下表格: Region Value Europe 5 Africa 6 America 7 Asia 8 我知道我可以通过以下方式查看地图: map.world <- map_data(map = "world") mapCountryData(sPDF,nameColumnToPlot = 'continent') map.world这是一个简化的大陆形状文件,您可以使用它。我会更改投影并删除南极洲,因为我会将其
Region Value
Europe 5
Africa 6
America 7
Asia 8
我知道我可以通过以下方式查看地图:
map.world <- map_data(map = "world")
mapCountryData(sPDF,nameColumnToPlot = 'continent')
map.world这是一个简化的大陆形状文件,您可以使用它。我会更改投影并删除南极洲,因为我会将其放入出版物中,但这给了您一个开始:
library(rgdal)
library(rgeos)
library(ggplot2)
library(httr)
url <- "https://gist.githubusercontent.com/hrbrmstr/91ea5cc9474286c72838/raw/f3fde312c9b816dff3994f39f2bcda03209eff8f/continents.json"
stop_for_status(GET(url, write_disk("continents.json")))
continents <- readOGR("continents.json", "OGRGeoJSON")
continents_map <- fortify(continents, region="CONTINENT")
data <- read.table(text="id value
Europe 5
Africa 6
America 7
Asia 8", header=TRUE, stringsAsFactors=FALSE)
gg <- ggplot()
gg <- gg + geom_map(data=continents_map,
map=continents_map,
aes(x=long, y=lat, map_id=id),
color="black")
gg <- gg + geom_map(data=data,
map=continents_map,
aes(map_id=id, fill=value),
color="black")
gg <- gg + scale_fill_distiller("PuBu") # needs latest ggplot2
gg <- gg + coord_equal()
gg <- gg + theme_bw()
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.grid=element_blank())
gg
所以你的github链接代表了“地理地图”的边界,对吗?好的,这是我一直在寻找的东西,但是我如何改变颜色呢?脚本的哪一部分是造成“蓝色”的原因,因为我无法理解…scale\u fill\u diller(“PuBu”)
我想这不是最漂亮的解决方案,但在某种程度上起到了作用。。。不知道还有什么更好的选择。@BlueSkies所以,正如我所说,这是一个基本的解决方案。在你的问题上,你几乎没有表现出任何努力(甚至连所需的库(rworldmap)
),我在其他SO帖子中也有很多其他的地理样式示例,包括如何更改投影和删除南极。关于github链接,是的。这是一个GeoJSON格式的shapefile(help(“readOGR”)
),是R中大多数“geo stuff”的基础。嗨,这个解决方案似乎不再有效了。如果我添加数据
部分,它将不会给出任何结果。如果我没有添加数据
,就可以了。有什么建议吗?谢谢
continents@data$CONTINENT
## [1] Asia North America Europe Africa South America
## [6] Oceania Australia Antarctica