在地图上绘制两组坐标

时间:2015-06-23 19:09:37

标签: r google-maps ggplot2 ggmap

我创建了两组矢量来在地图上绘制两组数据。

每次我跑,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)

给这个

enter image description here

我可以独立绘制点,即

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

2 个答案:

答案 0 :(得分:1)

如果认为你制造的东西比需要的东西更复杂。如果要将点颜色设置为某个分组变量,只需创建这样的变量即可。根据您在this question中发布的数据,您可以按以下方式执行此操作:

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

这给出了:

enter image description here

使用过的数据:

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

答案 1 :(得分:0)

我修复了代码。但是,如果您可以发布更优雅的代码并进行解释,我会将其标记为解决方案。

<div ng-controller="Test1Controller" data-ng-init="loadservice()">
<div ng-repeat="item in items">
<input type="checkbox" ng-model="item.SELECTED"  ng-true-value="'Y'" ng-false-value="'N'"/> {{item.D_Services}}
</div>
</div>

enter image description here

.csv文件的格式

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)
相关问题