gpt4 book ai didi

R - 使用 rworldmap 在 map 中打印特定的国家名称

转载 作者:行者123 更新时间:2023-12-02 00:52:24 25 4
gpt4 key购买 nike

我正在使用 R 中的 rworldmap 包中的欧洲 map 创建热图(因为我不知道如何使用 ggmap 或 ggplot2 执行此操作)。

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

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()

使用 labelCountries()打印所有国家/地区名称,但不可读。

谢谢

最佳答案

this 的帮助下之前的回答:

# 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))

您可以使用 text 添加国家/地区标签但您必须从匹配的坐标中提取坐标。
输出:

plot

你可能会玩一下 col = "color"text ,因为有些国家几乎无法阅读。或者可能更改 map 中的颜色比例

关于R - 使用 rworldmap 在 map 中打印特定的国家名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56623413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com