Я немного новичок в R и очень плохо знаком с любой из функций карты / ГИС в нем. Я хочу наложить файл формы с координатами, найденными в файле Excel. Я успешно создал карту нужных мне состояний и разместил над ней точки, и могу построить файл формы отдельно, но у меня проблемы с наложением всех трех вместе. Файлы форм можно найти здесь , а координаты дерева загружены здесь . Я полагаю, что у меня есть все части там, но не могу заставить их соответствовать друг другу.
library(xlsx)
library(plyr)
library(usmap)
library(ggplot2)
library(RColorBrewer)
library(scales)
#read in point-reference lat/long data in .xlsx format
treesplotrange=read.xlsx("trees_plot_range.xlsx",1)
#get US map corresponding only to states represented in the data set
#I know there is a more efficient/elegant way to do this part but
#I'm okay with this slightly cumbersome approach.
usa <- map_data("state")
usa <- usa[usa$region%in%c("alabama","ohio","new york",
"pennsylvania","delaware","indiana","michigan",
"maryland","virginia","west virginia","illinois",
"missouri","georgia","florida","north carolina",
"tennessee","kentucky","arkansas","kansas",
"mississippi","south carolina","texas",
"oklahoma","louisiana","nebraska"),]
#Plot to render the eastern US map
gg1 <- ggplot() +
geom_polygon(data = usa, aes(x=long, y = lat, group = group), fill = "gray", color = "blue") +
coord_fixed(1.3)
plot(gg1,add=TRUE)
#Create a data frame containing coordinates of the points to overlay
labs <- data.frame(
long = treesplotrange$LON,
lat = treesplotrange$LAT,
stringsAsFactors = FALSE
)
#Create a gradient of green colors corresponsing to the value
#of the response for each location to plot. Darker colors will
#indicate higher response value for a point.
colors=colorRampPalette(brewer.pal(6, "Greens"))(4)
qs=quantile(treesplotrange$NUMTREES,na.rm=TRUE,
probs=seq(0,1,.25))
plotcolors=rep(NA,length(treesplotrange$NUMTREES))
plotcolors[which(treesplotrange$NUMTREES<qs[2])]=colors[1]
plotcolors[which(treesplotrange$NUMTREES>qs[2]&
treesplotrange$NUMTREES<qs[3])]=colors[2]
plotcolors[which(treesplotrange$NUMTREES>qs[3]&
treesplotrange$NUMTREES<qs[4])]=colors[3]
plotcolors[which(treesplotrange$NUMTREES>qs[4]&
treesplotrange$NUMTREES<qs[5])]=colors[4]
#Overlay the color-gradient points
#A few missing observations excluded
gg1 +
geom_point(data = labs, aes(x = long, y = lat,
colour = treesplotrange$NUMTREES), color = plotcolors, size = 2)+
scale_colour_gradientn(colours = colors)
#Read in overlaid "natural range" shapefile
shape <- readOGR(dsn = "litt367av.shp" )
#View the shape I'd like toss overlay with translucent green.
#The Great Lakes can clearly be seen so I can see how
#it should fit over the previous plot.
plot(shape,col=alpha("green",.05))
#Main question: how can all three of these things - US
#map, color-coded points, and translucent shape, be
#plotted simultaneously?