在地理地图上绘制两组坐标
Plot two sets of coordinates on geographical map
我创建了两组向量来在地图上绘制两组数据。
每次我 运行,R Studio 都会崩溃。
我错过了什么?
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)
原代码
我的原始代码绘制了所有点
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)
给了这个
我可以独立绘制点,即
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")
我替换最后一行,plot(...
与
plot(under50.pts, pch = 22, col = "darkred")
我修改了代码。但是,如果你能post更优雅的代码并解释它,我会标记为解决方案。
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)
.csv 文件的格式
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
如果你认为你让事情变得比需要的更复杂。如果要将点着色为某个分组变量,只需创建这样一个变量即可。根据您在 中发布的数据,您可以执行以下操作:
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"))
这给出:
使用数据:
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")
我创建了两组向量来在地图上绘制两组数据。
每次我 运行,R Studio 都会崩溃。
我错过了什么?
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)
原代码
我的原始代码绘制了所有点
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)
给了这个
我可以独立绘制点,即
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")
我替换最后一行,plot(...
与
plot(under50.pts, pch = 22, col = "darkred")
我修改了代码。但是,如果你能post更优雅的代码并解释它,我会标记为解决方案。
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)
.csv 文件的格式
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
如果你认为你让事情变得比需要的更复杂。如果要将点着色为某个分组变量,只需创建这样一个变量即可。根据您在
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"))
这给出:
使用数据:
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")