【问题标题】:Names of the countries on the plot with rworldmap带有 rworldmap 的绘图上的国家/地区名称
【发布时间】:2015-02-12 04:42:35
【问题描述】:

早上好,我花了很多时间来弄清楚如何直接在情节上添加国家名称,而不是像传说的一部分,而是像地图的一部分。 我使用包 rworldmap,尝试使用 identifyCountries () - 但它用于交互(当用户点击地图时),而不是我找到了这样的解决方案 Administrative regions map of a country with ggmap and ggplot2 但它适用于 ggplot2,而且太复杂了。我试图用mapCountryData() 来做这件事。希望得到您的帮助,谢谢。

data <- data.frame(Country=c('Russia','Cyprus', 'Belize', 'Austria' ,'Virgin Islands', 
        'Italy','United States' ,'United Kingdom', 'Germany', 'France' ,'Poland' ,'Switzerland'),
Value=c(-0.310,-0.206,-0.300,-0.179,-0.196,-0.174,-0.105,-0.142,-0.082,-0.097,-0.027,0.052))

    library('rworldmap')

    pdf1 <- joinCountryData2Map(data, joinCode="NAME", nameJoinColumn="Country")

    mapCountryData(pdf1, nameColumnToPlot="Value", catMethod="pretty", 
    colourPalette='white2Black',addLegend='TRUE',mapTitle=NULL, mapRegion="Europe")

【问题讨论】:

    标签: r maps rworldmap


    【解决方案1】:

    你可以试试:

    # get the coordinates for each country
    country_coord<-data.frame(coordinates(pdf1),stringsAsFactors=F)
    # label the countries
    text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord))
    

    在你的代码之后传递这些行,你会得到:

    【讨论】:

    • 谢谢,我花了一些时间来弄清楚如何处理 country_coord 中的 row.names 以对我需要的国家进行子集化,但最后我做到了,谢谢你的帮助坐标,以及发短信的方式!!
    • @DimaSukhorukov,不客气!感谢您的反馈,我很高兴知道我的回答对您有所帮助:-)
    • 干得好。如果您愿意,您可以使用更少的代码获得相同的结果。 text(pdf1, labels="NAME")
    • @Andy,谢谢,很高兴知道!虽然,通过你的代码,我收到一个错误cannot coerce type 'S4' to vector of type 'double'...
    • @CathG 啊哈!是的,我刚刚意识到这只有在您还加载了 raster 包时才有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多