简体   繁体   中英

Plot two sets of coordinates on geographical map

I created two sets of vectors to plot two sets of data on a map.

Everytime I run, R Studio crashes.

What am I missing?

library(ggmap)
setwd("d:/GIS/31R")
sep <- read.csv("California_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.11", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
library(sp)


lst <- split(sep, sep[,8] >= 50)
under50 <- lst[[1]]
over50 <- lst[[2]] 

coords <- cbind(Longitude = as.numeric(as.character(under50$Longitude)),Latitude=as.numeric(as.character(under50$Latitude)))
coords2 <- cbind(Longitude2 = as.numeric(as.character(over50$Longitude)),Latitude2=as.numeric(as.character(over50$Latitude)))


map <- qmap('Yorba Linda', zoom = 11, maptype = 'hybrid')


map + geom_point(data=under50, aes(x = Longitude, y = Latitude), color="red", size = 5, alpha = 0.5) + geom_point(data=over50, aes(x = Longitude2, y = Latitude2), color="green", size = 5, alpha = 0.5)

Original Code

My original code plotted all points

library(ggmap)
setwd("d:/GIS/31R")
sep <- read.csv("California_SEP_assets_csv.csv")
library(sp)
coords <- cbind(Longitude = as.numeric(as.character(sep$Longitude)),Latitude=as.numeric(as.character(sep$Latitude)))
sep.pts <- SpatialPointsDataFrame(coords,sep[,-(2:3)],proj4string = CRS("+init=epsg:4326"))
plot(sep.pts, pch=".",col="darkred")
map <- qmap('Yorba Linda', zoom = 11, maptype = 'hybrid')
map + geom_point(data=sep, aes(x = Longitude, y = Latitude), color="red", size = 5, alpha = 0.5)

Gave this

在此处输入图片说明

I am able to plot points standalone, ie

library(ggmap)
setwd("d:/GIS/31R")
sep <- read.csv("California_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.11", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
library(sp)


lst <- split(sep, sep[,8] >= 50)
under50 <- lst[[1]]
over50 <- lst[[2]] 

coords <- cbind(Longitude = as.numeric(as.character(under50$Longitude)),Latitude=as.numeric(as.character(under50$Latitude)))
under50.pts <- SpatialPointsDataFrame(coords, under50[, -(2:3)], proj4string = CRS("+init=epsg:4326"))

coords2 <- cbind(Longitude2 = as.numeric(as.character(over50$Longitude)),Latitude2=as.numeric(as.character(over50$Latitude)))
over50.pts <- SpatialPointsDataFrame(coords2, over50[, -(2:3)], proj4string = CRS("+init=epsg:4326"))

plot(over50.pts, pch = 22, col = "darkgreen")

and I replace the last line, plot(...

with

plot(under50.pts, pch = 22, col = "darkred")

If think you are making things more complicated than needs to be. If you want to color the points to a certain grouping variable, just create such a variable. Based on the data you posted in this question , you can do this as follows:

library(ggmap)
library(ggplot2)

# create a new grouping variable
sep$newvar <- ifelse(sep[,8] >= 50, "Over 50", "Under 50")

# get the map
map <- get_map('Yorba Linda', zoom = 11, maptype = 'hybrid')

# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
  geom_point(data=sep, aes(x = Longitude, y = Latitude, color=newvar), size=7, alpha=0.6) +
  scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))

this gives:

在此处输入图片说明

Used data:

sep <- structure(list(Site = structure(1:6, .Label = c("31R001", "31R002", "31R003", "31R004", "31R005", "31R006"), class = "factor"),
                      Latitude = c(33.808874, 33.877256, 33.820825, 33.852373, 33.829697, 33.810274), 
                      Longitude = c(-117.844048, -117.700135, -117.811845, -117.795516, -117.787532, -117.830429), 
                      Windows.SEP.11 = c(63L, 174L, 11L, 85L, 163L, 71L), 
                      Mac.SEP.11 = c(0L, 1L, 4L, 0L, 0L, 50L), 
                      Windows.SEP.12 = c(124L, 185L, 9L, 75L, 23L, 5L), 
                      Mac.SEP.12 = c(0L, 1L, 32L, 1L, 0L, 50L), 
                      newCol = c(33.6898395721925, 48.4764542936288, 26.7857142857143, 52.7950310559006, 87.6344086021505, 68.75), 
                      newvar = c("Under 50", "Under 50", "Under 50", "Over 50", "Over 50", "Over 50")), 
                 .Names = c("Site", "Latitude", "Longitude", "Windows.SEP.11", "Mac.SEP.11", "Windows.SEP.12", "Mac.SEP.12","newCol", "newvar"), 
                 row.names = c(NA, 6L), class = "data.frame")

I fixed the code. However, if you can post more elegant code and explain it, I will mark as solution.

library(ggmap)
setwd("d:/GIS/31R")
sep <- read.csv("California_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.11", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
library(sp)


lst <- split(sep, sep[,8] >= 50)
under50 <- lst[[1]]
over50 <- lst[[2]] 

coords <- cbind(Longitude = as.numeric(as.character(under50$Longitude)),Latitude=as.numeric(as.character(under50$Latitude)))
under50.pts <- SpatialPointsDataFrame(coords, under50[, -(2:3)], proj4string = CRS("+init=epsg:4326"))

coords2 <- cbind(Longitude = as.numeric(as.character(over50$Longitude)),Latitude=as.numeric(as.character(over50$Latitude)))
over50.pts <- SpatialPointsDataFrame(coords2, over50[, -(2:3)], proj4string = CRS("+init=epsg:4326"))

map <- qmap('Yorba Linda', zoom = 11, maptype = 'hybrid')

map + geom_point(data=over50, aes(x = Longitude, y = Latitude), color="green", size = 5, alpha = 0.5) + geom_point(data=under50, aes(x = Longitude, y = Latitude), color="red", size = 5, alpha = 0.5)

在此处输入图片说明

Format of the .csv file

    Site Latitude Longitude Windows.SEP.11 Mac.SEP.11 Windows.SEP.12 Mac.SEP.12   newCol
1 31R001 33.80887 -117.8440             63          0            124          0 33.68984
2 31R002 33.87726 -117.7001            174          1            185          1 48.47645
3 31R003 33.82082 -117.8118             11          4              9         32 26.78571
4 31R004 33.85237 -117.7955             85          0             75          1 52.79503
5 31R005 33.82970 -117.7875            163          0             23          0 87.63441
6 31R006 33.81027 -117.8304             71         50              5         50 68.75000

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