R-使用rworldmap在地图上打印特定的国家名称 [英] R - Print specific country names in a map using rworldmap

查看:66
本文介绍了R-使用rworldmap在地图上打印特定的国家名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中的rworldmap包中使用欧洲地图创建一个热图(因为我不知道如何使用ggmap或ggplot2来做到这一点).

我只需要绘制数据框中存在的国家/地区的国名,而不是所有欧洲国家/地区的国名.我怎样才能做到这一点?我的代码:

 库(RColorBrewer)#获取颜色colourPalette<-brewer.pal(5,'RdPu')图书馆(rworldmap)欧洲<-data.frame(国家/地区" = c(希腊",法国",西班牙",意大利",英国",芬兰",挪威",瑞典",德国",罗马尼亚"),"x" = c(2.5,3,2.2,1.8,2.32,1.99,2.01,2.34,1.88,2.45))匹配的<-joinCountryData2Map(欧洲,joinCode ="NAME",nameJoinColumn ="country")mapParams<-mapCountryData(matched,nameColumnToPlot ="x",mapTitle ="my Titley",addLegend = FALSE,mapRegion ="Europe",colourPalette = colourPalette,oceanCol =#404040",missingCountryCol =#A0A0A0")#添加图例do.call(addMapLegend,c(mapParams,legendLabels ="all",legendWidth = 0.5,legendIntervals =数据",legendMar = 2))labelCountries() 

使用 labelCountries()打印所有国家名称,并且不可读.

谢谢

解决方案

您可能会在 text 中使用 col ="color" 玩一点,因为几乎无法读取某个国家/地区.或更改地图中的颜色比例

I am creating a heatmap using the map of Europe in rworldmap package in R (since I don't know how to do this with ggmap or ggplot2).

I need to plot the country names of the countries that are present in my dataframe only, not all european countries. How can I do this? My code:

library(RColorBrewer)
#getting colours
colourPalette <- brewer.pal(5,'RdPu')


library(rworldmap)

europe <- data.frame(
  "country" = c("Greece", 
                "France", 
                "Spain",
                "Italy",
                "UK",
                "Finland","Norway","Sweden",
                "Germany",
                "Romania"), 
  "x" = c(2.5, 3, 2.2, 1.8,2.32, 1.99, 2.01, 2.34, 1.88, 2.45))

matched <- joinCountryData2Map(europe, joinCode="NAME", nameJoinColumn="country")





mapParams <- mapCountryData(matched, 
                            nameColumnToPlot="x", 
                            mapTitle="my Titley", 
                            addLegend=FALSE,
                            mapRegion="Europe"
                            ,colourPalette=colourPalette,
                            oceanCol="#404040", missingCountryCol="#A0A0A0")



#adding legend
do.call(addMapLegend
        ,c(mapParams
           ,legendLabels="all"
           ,legendWidth=0.5
           ,legendIntervals="data"
           ,legendMar = 2))

labelCountries()

Using labelCountries() prints all country names and it's not readable.

Thanks

解决方案

With a little bit help of this previously answer:

# get the coordinates for each country
country_coord<-data.frame(coordinates(matched),stringsAsFactors=F)

# label the countries
country = c("Greece", 
              "France", 
              "Spain",
              "Italy",
              "UK",
              "Finland","Norway","Sweden",
              "Germany",
              "Romania")

#filter your wanted countrys
country_coord = country_coord[country,]

#insert your labels in plot
text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord))

you can add the country labels with text but you must extract the coordinates before from your matched coordinates. Output:

You might play a bit with col = "color" in text, since some country can barely been read. Or maybe change the color scale in your map

这篇关于R-使用rworldmap在地图上打印特定的国家名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆