【问题标题】:How to plot an Europe map with my own data?如何用我自己的数据绘制欧洲地图?
【发布时间】:2021-08-08 20:51:20
【问题描述】:

我想做一张欧洲地图,所以我尝试使用这段代码,但我真的不知道它是如何工作的。

#that's my data

europe<-structure(list(Code = c("BE", "BG", "CZ", "DK", "DE", "EE", "IE", 
"EL", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", 
"NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE", "IS", "NO", 
"CH", "UK"), Country = c("Belgium", "Bulgaria", "Czechia", "Denmark", 
"Germany (until 1990 former territory of the FRG)", "Estonia", 
"Ireland", "Greece", "Spain", "France", "Croatia", "Italy", "Cyprus", 
"Latvia", "Lithuania", "Luxembourg", "Hungary", "Malta", "Netherlands", 
"Austria", "Poland", "Portugal", "Romania", "Slovenia", "Slovakia", 
"Finland", "Sweden", "Iceland", "Norway", "Switzerland", "United Kingdom"
), Production = c(133.2, 48.2, 77.4, 138.2, 121.3, 71.2, 178.9, 
58.5, 95, 126.1, 64.5, 100.1, 75, 59.8, 67.7, 175.1, 66.8, 75.8, 
122.3, 115.7, 65, 66.1, 66.1, 83.9, 70.1, 109.5, 112.4, 118.2, 
152.4, 129.3, 96.8)), row.names = c(NA, -31L), class = c("tbl_df", 
"tbl", "data.frame"))

现在我正在使用我找到的这段代码,我知道我的问题是当我绑定数据时,如果有人可以告诉我如何解决它或者你是否知道其他方法...... :)

# Download the shape file from the web and unzip it:
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="world_shape_file.zip")
system("unzip world_shape_file.zip")

# Load it as a geospatial object in R
library(rgdal)
my_spdf <- readOGR( dsn= "./world_shape_file/" , layer="TM_WORLD_BORDERS_SIMPL-0.3", verbose=FALSE) 
europa <- my_spdf[my_spdf@data$REGION==150 , ]

europa@data <- merge(europa@data, europe)



# Use the cartography library to do the choropleth map

library(maptools)

par(mar=c(0,0,0,0))

spplot(europa, zcol = "Production",xlim=c(-30,120) , ylim=c(30,90), lwd=0.5)

``

【问题讨论】:

    标签: r maps rgdal maptools


    【解决方案1】:

    我认为你认为欧罗巴物体仍然有一个名为 @data 的项目,但如果你看一下,情况并非如此:

    > my_spdf <- readOGR( dsn= "~/Downloads/" , layer="TM_WORLD_BORDERS_SIMPL-0.3", verbose=FALSE) 
    > europa <- my_spdf[my_spdf@data$REGION==150 , ]
    > names(europa)
     [1] "FIPS"      "ISO2"      "ISO3"      "UN"        "NAME"      "AREA"      "POP2005"  
     [8] "REGION"    "SUBREGION" "LON"       "LAT"   
    

    此外,您要合并的列的名称在 europa 和 europe 中是不同的,因此您需要告诉合并并保留所有区域由 all.x=TRUE 完成

    > europa <- merge(europa, europe, by.x="FIPS",by.y="Code", all.x=TRUE)
    > names(europa)
     [1] "FIPS"       "ISO2"       "ISO3"       "UN"         "NAME"       "AREA"       "POP2005"   
     [8] "REGION"     "SUBREGION"  "LON"        "LAT"        "Country"    "Production"
    

    现在你可以绘图了:

     png()
     spplot(europa, zcol = "Production",xlim=c(-30,120) , ylim=c(30,90), lwd=0.5)
     dev.off()
    

    【讨论】:

      猜你喜欢
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      相关资源
      最近更新 更多