R 3.2.1 颜色映射不正确

R 3.2.1 incorrect mapping of color

这是基于

我有两个数据点,一个大于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")

输出是这样的(错误的红圈).......如何解决?

回应

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

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[=13=]L001  40.821367   -73.488313  4   0   3   16
Out of City\O308    41.310426   -73.837612  69  1   26  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%"

问题是默认情况下 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"))

显然你也可以两者兼顾。