R 3.2.1颜色映射不正确

时间:2015-07-09 16:38:22

标签: r google-maps ggmap

这基于R 3.2.1, reverse colors on map

我有两个数据点,一个超过66%,应该是绿色,其他小于33%,应该是红色。

但是,不到33%是橙色。

下面是代码,看起来是正确的(但有些不对劲)

sep <- read.csv("Out_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])

# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Less than 33%", ifelse(sep[,8] >= 66, "More than 66%", "Between 33% and 66%"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
                               levels = c("More than 66%", "Between 33% and 66%", "Less than 33%"))


# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 1)
map <- get_map(bbox)


# 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=Percent_SEP12_Assets ), size=9, alpha=0.6) +
  scale_color_manual(values=c("green","orange","red"))

dput(sep)是

structure(list(School = structure(1:2, .Label = c("Out of City\\00L001", 
"Out of City\\O308"), class = "factor"), Latitude = c(40.821367, 
41.310426), Longitude = c(-73.488313, -73.837612), Windows.SEP.11 = c(4L, 
69L), Mac.SEP.11 = 0:1, Windows.SEP.12 = c(3L, 26L), Mac.SEP.12 = c(16L, 
1L), newCol = c(82.6086956521739, 27.8350515463918)), .Names = c("School", 
"Latitude", "Longitude", "Windows.SEP.11", "Mac.SEP.11", "Windows.SEP.12", 
"Mac.SEP.12", "newCol"), row.names = c(NA, -2L), class = "data.frame")

输出是这个(不正确用红色圈出)........如何解决?

enter image description here

的响应

坐标是正确的,我在问为什么这个点的颜色不正确。我认为这个逻辑是正确的

Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Less than 33%", ifelse(sep[,8] >= 66, "More than 66%", "Between 33% and 66%"))

更新了代码

我为@bondeded用户尝试了此操作,结果地图与之前相同

sep <- read.csv("Out_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])



# create a new grouping variable

sep$Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Less than 33%", ifelse(sep[,8] >= 66, "More than 66%", "Between 33% and 66%"))
sep$Percent_SEP12_Assets <- factor(sep$Percent_SEP12_Assets,
                               levels = c("More than 66%", "Between 33% and 66%", "Less than 33%"))


# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 1)
map <- get_map(bbox)


# 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=sep$Percent_SEP12_Assets ), size=9, alpha=0.6) +
  scale_color_manual(values=c("green","orange","red"))

实际CSV

这是实际的CSV,两行

School               Latitude   Longitude   Windows-SEP-11  Mac-SEP-11  Windows-SEP-12  Mac-SEP-12
Out of City\00L001  40.821367   -73.488313  4   0   3   16
Out of City\O308    41.310426   -73.837612  69  1   26  1

2 个答案:

答案 0 :(得分:2)

问题在于,默认情况下ggplot2会从因子中删除未使用的级别。有两种选择:

指定drop = FALSE

ggmap(map) +
  geom_point(data=sep, aes(x = Longitude, y = Latitude, color=sep$Percent_SEP12_Assets ), size=9, alpha=0.6) +
  scale_color_manual(values=c("green","orange","red"), drop = FALSE)

指定每个级别的值:

ggmap(map) +
  geom_point(data=sep, aes(x = Longitude, y = Latitude, color=sep$Percent_SEP12_Assets ), size=9, alpha=0.6) +
  scale_color_manual(values=c(`More than 66%` = "green", `Between 33% and 66%` = "orange", `Less than 33%` = "red"))
显然,你也可以做到这两点。

答案 1 :(得分:1)

现在我明白了你的意思。问题在于你的ifelse结构。也许这可以提供帮助:

ifelse(sep[,8] <= 33, "Less than 33%", ifelse(sep[,8] >= 66, "More than 66%", "Between 33% and 66%"))
    [1] "More than 66%" "Less than 33%"