简体   繁体   中英

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

Friends, I created a scatterplot with the code below. However the points plotted on the scatterplot I would like them to be shown on a map that is in shapefile. It is possible? The scatter code example is below.

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


SCATTER PLOT

在此处输入图像描述

# 在此处输入图像描述 Thank you very much friends!

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

I think something like this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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