我正在查看一个国家/地区中的区域数据。我需要对许多数据集执行此操作(但在同一国家/地区)。我有一个有效的方法,该方法基于“使用'sp'程序包(格子图)在R中构建地图”部分中的以下代码http://biblogg.no/2015/05/18/building-a-map-of-norway-in-r/构建。这工作正常,非常整洁。该方法使用了SP程序包,并且可以处理空间多边形。
但是,考虑到它做的很少,它非常慢(Excel在十分之一的时间内做了同样的事情)。这使我想知道,当我的每个图的数据集每个区域仅包含一个整数时,是否存在一种更简单的方法。我以前使用过“ map_data”功能来进行国家/地区地图绘制,但这似乎不适用于所有国家/地区的地区
编辑:发布使用的数据
norway2 <- readOGR(dsn="/Users/isa/Documents/Courses/Johns Hopkins/RKart_Norge/NOR_adm" ,
layer="NOR_adm2")
# We take a look at the data
norway2_data <- norway2@data
str(norway2_data); head(norway2_data)
# We create a dataframe for the churn rate per regions
d <- c( "Akershus" , "Aust-Agder", "Buskerud", "Finnmark", "Hedmark" , "Hordaland", "Møre og Romsdal" , "Nord-Trøndelag" , "Nordland" , "Oppland", "Oslo", "Ãstfold", "Rogaland", "Sogn og Fjordane", "Sør-Trøndelag", "Telemark", "Troms", "Vest-Agder" , "Vestfold" )
e <- c(1.0, 1.1, 1.5, 1.55, 2.9, 3.12, 3.1, 4.2, 4.3, 4.8, 5.1, 5.3, 5.5, 5.56, 7.9, 8.3, 11, 5.6, 6.1)
name3 <- c("NAME_1", "Churn"); dt2 <- as.data.frame(cbind(d, e), stringsAsFactors=FALSE)
dt2$e <- as.numeric(dt2$e); colnames(dt2) <- name3; churn <- dt2
# We plot the Norwegian regions using the unionSpatialPolygons function from the 'maptools' package
IDs <- norway2_data$ID_1
# We merge Polygons
norway3_new <- unionSpatialPolygons(norway2, IDs)
# We build the new SpatialPolygonsDataFrame with the churn rate
norway4_new <- SpatialPolygonsDataFrame(norway3_new, churn)
# Then you can use spplot to visualize the Norwegian regions with their respective churn rate
# We define the color palette
pal2 <- colorRampPalette(c("white", "red"))
# Remove the plot frame
trellis.par.set(axis.line=list(col=NA))
# Plot the regions with Lattice
spplot(norway4_new, "Churn", main="Churn Rate per Norwegian Region (Fylke)",
lwd=.4, col="white", col.regions=pal2(19), colorkey = FALSE, bty="n")