gpt4 book ai didi

R 映射 : how do I fill the regions?

转载 作者:行者123 更新时间:2023-12-01 02:27:22 24 4
gpt4 key购买 nike

我在这里绘制 map ,无法正确填写,希望您能提供帮助。那我该怎么办

阅读 map 并更改投影:

map<-readOGR(dsn="e:\\r\\OSM_adm_reg\\adm4_region (1)", layer="adm4_region")
map=spTransform(map,CRS("+proj=latlong +ellips=GRS80"))

阅读关于物流仓库的位置数据,在全国各地和匹配world.cities数据库绘制他们在 map 上:
    data(world.cities)
russian.cities=world.cities[world.cities$country.etc=="Russia",]
metro_outlets=read.csv(file="e:\\r\\retail\\metro.csv",head=TRUE,sep=";")
match<-match(metro_outlets$CITY_ENGLISH, russian.cities$name)
outlet_coordinates=russian.cities[match,]
outlet_coordinates=cbind(outlet_coordinates, "metro_store_count"=metro_outlets$STORE_COUNT_METRO)
outlet_coordinates=cbind(outlet_coordinates, "cch_depo"=metro_outlets$DEPO_FOOD)
outlet_coordinates=cbind(outlet_coordinates, "NAME_LAT"=metro_outlets$STORE_OBLAST)
outlet_coordinates=cbind(outlet_coordinates, "cch_franchise"=metro_outlets$Franchise)
outlet_coordinates=cbind(outlet_coordinates, "SL"=metro_outlets$SL)

基本上,我需要根据 SL 列的值填充区域。
现在,我试图获取包含 WH 的区域列表:
outlet_spatial=SpatialPoints(outlet_coordinates[,c(5,4)],proj4string=CRS(proj4string(map)))
index=over(outlet_spatial,map)
region_names=rownames(map@data[map@data$NAME_LAT %in% index$NAME_LAT,])
map_df<-fortify(map,map$NAME_LAT)
map_df$fill=ifelse(map_df$id %in% region_names,"true","false")

并绘制 map :
ggplot()+
geom_polygon(data=map_df,aes(x=long,y=lat,group=group,fill=fill),col="gray")+
scale_fill_manual(values=c("gray","gray70"))+xlim(20, 180) + ylim(40,80)+
geom_point(aes(x=long,y=lat), color="orange",alpha=I(8/10), data=outlet_coordinates)

这是我的 map got !

现在,我不知道如何将 SL 列的值传递给 map_df并相应地填充区域。此处定义的 SL: outlet_coordinates=cbind(outlet_coordinates,"SL"=metro_outlets$SL)

最佳答案

这比我想象的要复杂一些,主要是因为您得到的拓扑错误是由 shapefile 中的几何图形不佳引起的。也许您可以改用高质量的、公开可用的 shapefile。我建议 www.naturaleartchdata.com .我在脚本中包含了一个链接并下载了该数据。您可以复制并粘贴此内容。这应该有效:

require(ggplot2)
require(maptools)
require(sp)

# Your shape file has some topology errors meaning it can't be used by 'over()'
# Use open source shapefiles from www.naturalearth.com - v. good quality
oldwd <- getwd(); tmp <- tempdir(); setwd(tmp)
url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_1_states_provinces_shp.zip"
dest <- paste(tmp,"\\tmp.zip",sep="")
download.file( url , dest )
unzip(dest)


# Projection information for geographic data (but in decimal degrees)
projgeo <- CRS("+proj=latlong +datum=WGS84 +ellps=WGS84")


# Read in world shapefile
wld <- readShapePoly("ne_10m_admin_1_states_provinces_shp" , proj4string = projgeo)
# Subset to Russia
map <- wld[wld$admin == "Russia" , ]


# City and metro data
r.city <- world.cities[ world.cities$country.etc=="Russia" , ]
metro_outlets <- read.csv(file="e:\\r\\retail\\metro.csv",head=TRUE,sep=";")


# Assign metro outlets to city and make spatial point data frame
met <- subset( metro_outlets , select = c( "SL" , "CITY_ENGLISH" ) )
met$SL <- as.numeric( met$SL )
match <- match( met$CITY_ENGLISH , r.city$name )
coordinates( met ) <- r.city[ match , c( "long" , "lat" ) ]
proj4string( met ) <- proj4string( map )


# Assign metro outlet attribute "SL" to each region of map
# Find the average SL for each region using 'over()'
df <- over( map , met[ , "SL" ] , fn = mean , na.rm = TRUE )
map$SL <- as.numeric( df$SL )


# Convert th map into format for plotting with ggplot
map@data$id <- rownames( map@data )
map.df <- as.data.frame( map )
map.fort <- fortify( map , region = "id" )
map.gg <- join( map.fort , map.df , by = "id" )


# Plot
p1 <- ggplot( NULL ) +
geom_polygon( mapping = aes( long , lat , group = group , fill = factor( SL ) ) , colour = "white" , data = map.gg , map = map.gg )+
scale_x_continuous( limits = c( 20 , 180 ) )
print(p1)

enter image description here

希望有帮助!

关于R 映射 : how do I fill the regions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15313273/

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