1

我对使用 R 处理空间数据非常陌生。所以我希望有人能指出我在哪里犯了错误。

该地图在没有 geom_text 元素的情况下完美运行。但是,当我尝试在地图上标记不同区域时,整个图都变黑了。

#First function loads spacial data, second loads arguments data
geo_data <- readOGR(dsn = file.choose(), layer = "CIV_adm01")
population_data <- read.csv(file.choose(), stringsAsFactors = `FALSE) #load         

新属性 population_data <- as.factor(population_data$phones)

# merge on common variable, here called 'NAME_1'
m <- merge(geo_data, population_data, by='NAME_1')

# saves shapefile in directory
shapefile(m, "path/merged.shp")

m_f <- fortify(m) #defines polygons, but loses atributes of the data
m$id <- row.names(m) #needed for join, extracts ID column
m_f <- left_join(m_f, m@data) # join the data

#change "fill" to variable you wish to analyze
map <- ggplot(m_f, aes(long, lat, group = group, fill = phones)) +
geom_polygon() +
coord_equal() +
geom_text(aes(x=long,y=lat,label=NAME_1), data = m_f, size=3, alpha = 0.3) +
labs(x = "", y = "",
fill = "Concentration of Phones") + #title of legend
ggtitle("Phones per region") #title of map

#Map Style 1: Blue color
map + scale_fill_gradient(high = "#132B43", low = "#56B1F7", space = "Lab",
                      na.value = "grey50", guide = "colourbar")

这是我得到的结果。

在此处输入图像描述

4

0 回答 0