【问题标题】:Plot Points of the scatter plot on a shapefile map in R在 R 中的 shapefile 地图上绘制散点图
【发布时间】:2020-04-30 22:49:25
【问题描述】:

朋友们,我用下面的代码创建了一个散点图。但是,在散点图上绘制的点我希望它们显示在 shapefile 中的地图上。有可能的?分散代码示例如下。

library(readxl)
library(rdist)
library(ggplot2)
library(geosphere)
library(tidyverse)

df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19), Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,  -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, 
                                    + -23.9, -23.9, -23.9, -23.9, -23.9), Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.7, 
                                    + -49.7, -49.7, -49.7, -49.7, -49.6, -49.6, -49.6, -49.6), Waste = c(526, 350, 526, 469, 285, 175, 175, 350, 350, 175, 350, 175, 175, 364, 
                                    + 175, 175, 350, 45.5, 54.6)), class = "data.frame", row.names = c(NA, -19L))

#cluster
coordinates<-df[c("Latitude","Longitude")]
d<-as.dist(distm(coordinates[,2:1]))
fit.average<-hclust(d,method="average") 

#Number of clusters
clusters<-cutree(fit.average, 2) 
nclusters<-matrix(table(clusters))  
df$cluster <- clusters 

#Localization
center_mass<-matrix(nrow=2,ncol=2)
for(i in 1:2){
center_mass[i,]<-c(weighted.mean(subset(df,cluster==i)$Latitude,subset(df,cluster==i)$Waste),
weighted.mean(subset(df,cluster==i)$Longitude,subset(df,cluster==i)$Waste))}
coordinates$cluster<-clusters 
center_mass<-cbind(center_mass,matrix(c(1:2),ncol=1)) 


#Scatter Plot
suppressPackageStartupMessages(library(ggplot2))
df1<-as.data.frame(center_mass)
colnames(df1) <-c("Latitude", "Longitude", "cluster")
g<-ggplot(data=df,  aes(x=Longitude, y=Latitude,  color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
Centro_View<- g +  geom_text(data=df, mapping=aes(x=eval(Longitude), y=eval(Latitude), label=Waste), size=3, hjust=-0.1)+ geom_point(data=df1, mapping=aes(Longitude, Latitude), color= "green", size=4) + geom_text(data=df1, mapping = aes(x=Longitude, y=Latitude, label = 1:2), color = "black", size = 4)
plot1<-print(Centro_View + ggtitle("Scatter Plot") + theme(plot.title = element_text(hjust = 0.5)))


散点图

# 非常感谢朋友们!

【问题讨论】:

  • 您可能会考虑使用sf 包来绘制边界文件和点
  • 感谢您的回答。你能告诉我如何通过你所说的那个包解决我上面的问题吗?

标签: r scatter-plot shapefile


【解决方案1】:

您应该能够使用 rgdal 包读取 shapefile,然后使用 ggplot2 绘制它:https://www.r-graph-gallery.com/168-load-a-shape-file-into-r.html

我认为这样的事情应该可行:

library(rgdal)
library(broom)
library(ggplot2)

# Read shape file with the rgdal library
my_spdf <- readOGR( 
  dsn = "./folder_w_shapefile",
  layer = "your_shapefile" # Do not need ".shp" file extension
)

# 'Fortify' the data to get a dataframe format required by ggplot2
spdf_fortified <- tidy(my_spdf)

# Plot it
ggplot() +
  geom_polygon(data = spdf_fortified, aes(x = long, y = lat, group = group),
  fill = "#69b3a2", color = "white") +
  geom_point(data = df, aes(x = Longitude, y = Latitude, color = factor(clusters)),
  size = 4) +
  theme_void()

【讨论】:

  • 感谢您的回答。我把你的推荐推荐给了朋友,但屏幕是空白的,如上所示。我的意图是绘制我在上面插入的散点图,并将其放置在 shapefile 地图上。我做了你要求的一切,只是显示屏幕空白。再次感谢
猜你喜欢
  • 2021-10-29
  • 2020-08-17
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多