评估矩阵

时间:2015-06-22 19:43:54

标签: r matrix ggplot2

我正在尝试评估矩阵的第8列

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)
coords <- cbind(Longitude = as.numeric(as.character(sep$Longitude)),Latitude=as.numeric(as.character(sep$Latitude)))

if (sep[8] > 50){
  sep.pts <- SpatialPointsDataFrame(coords,sep[,-(2:3)],proj4string = CRS("+init=epsg:4326"))
} else  {
  sep2.pts <- SpatialPointsDataFrame(coords,sep[,-(2:3)],proj4string = CRS("+init=epsg:4326"))
}

我收到警告

the condition has length > 1 and only the first element will be used

评估单个列下每个条目的语法是什么?

这是矩阵

dput(head(sep))
structure(list(Site = structure(1:6, .Label = c("31R001", "31R002", 
"31R003", "31R004", "31R005", "31R006", "31R007", "31R008", "31R011", 
"31R013", "31R014", "31R016", "31R018", "31R019", "31R020", "31R021", 
"31R022", "31R023", "31R024", "31R025", "31R026", "31R027", "31R029", 
"31R030", "31R031", "31R032", "31R034", "31R035", "31R036", "31R038", 
"31R039", "31R040", "31R041", "31R042", "31R043", "31R044", "31R045", 
"31R046", "31R048", "31R049", "31R050", "31R051", "31R052", "31R053", 
"31R054", "31R055", "31R056", "31R057", "31R058", "31R059", "31R060", 
"31R061", "31R069", "31R071", "31R072", "31R075", "31R435", "31R440", 
"31R445", "31R450", "31R455", "31R460", "31R470", "31R600", "31R722", 
"31R801", "31R825", "31R826", "31R829", "31R840", "31R843", "31R861", 
"31R880"), 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)), .Names = c("Site", "Latitude", "Longitude", "Windows.SEP.11", 
"Mac.SEP.11", "Windows.SEP.12", "Mac.SEP.12", "newCol"), row.names = c(NA, 
6L), class = "data.frame")

最后,我想在Google地图上对图表进行颜色编码,其中>= 50为红色,< 50%为绿色

更多信息

我的原始代码只给出了红点,无论

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

另一次更新

我更改了代码以从SpatialPointsDataFrame中删除,-(2:3)。我真的不明白删除经度和纬度列的重要性,但至少它不会给出错误。

但是,我正在研究如何在地图上绘制SpatialPointsDataFrame。

这是迄今为止的代码

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)
coords <- cbind(Longitude = as.numeric(as.character(sep$Longitude)),Latitude=as.numeric(as.character(sep$Latitude)))


sep.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol >= 50], proj4string = CRS("+init=epsg:4326"))
sep2.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol < 50], proj4string = CRS("+init=epsg:4326"))

map <- qmap('Yorba Linda', zoom = 11, maptype = 'hybrid')
map + geom_point(data=sep.pts, aes(x = Longitude, y = Latitude), color="red", size = 5, alpha = 0.5) + geom_point(data=sep2.pts, aes(x = Longitude, y = Latitude), color="green", size = 5, alpha = 0.5)

并且它给出了错误

Error: ggplot2 doesn't know how to deal with data of class SpatialPointsDataFrame

2 个答案:

答案 0 :(得分:1)

sep.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol >= 50, -(2:3)],proj4string = CRS("+init=epsg:4326"))
sep2.pts <- SpatialPointsDataFrame(coords,sep[sep.pts$newCol < 50,-(2:3)],proj4string = CRS("+init=epsg:4326"))

我假设您想要的最后一行&gt; =和&lt; 50而不是&gt;和&lt; =。

您需要将逻辑向量作为第一个参数传递给子集运算符,如下所示。

答案 1 :(得分:1)

ggplot2不是我的专长,但隐含条件的结构如下:

如果我的数据框为df <- data.frame(x=1:3, y=4:6)

df
  x y
1 1 4
2 2 5
3 3 6

我可以为x大于1的值和不大于1的值创建两个数据帧。我希望它看起来像这样:

df1
  x y
1 1 4

df2
  x y
2 2 5
3 3 6

我可以到达那里:

split(df, df$x > 1)
$`FALSE`
  x y
1 1 4

$`TRUE`
  x y
2 2 5
3 3 6

更新 -

我们可以将函数分配给变量。

lst <- split(df, df$x > 1)
df1 <- lst[[1]]
df2 <- lst[[2]]

在你的情况下,

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