繁体   English   中英

Plot 散点 plot 在形状文件 map 在 ZE1E1D3D4050CAF127D6EE048

[英]Plot Points of the scatter plot on a shapefile map in R

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

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)))


散射 PLOT

在此处输入图像描述

# 在此处输入图像描述 非常感谢各位朋友!

You should be able to read the shapefile with the rgdal package and then plot it using 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM