【问题标题】:R 3.2.1, reverse colors on mapR 3.2.1,在地图上反转颜色
【发布时间】:2015-07-07 15:18:00
【问题描述】:

这是R代码的一部分

# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))

# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
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=5, alpha=0.6) +
  scale_color_manual(breaks=c("Upper Third", "Middle Third","Lower Third"), values=c("green","orange","red"))

但不是制作地图

“上三分之一”对应绿色 “中三”对应Orange “下三”对应红色

配色方案混淆了,即上三分之一对应红色,下三分之一对应绿色。

数字越大 = 好 = 绿色,但地图显示相反。如何解决这个问题?

到目前为止我尝试了什么

以下代码

# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets)
levels(Percent_SEP12_Assets) <- c("Upper Third", "Middle Third", "Lower Third")


# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
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"))

会给出这个,它会更正数据标签,但地图中的点是倒置的,即绿色的地方是红色的,反之亦然(蓝色圆圈中的点应该是红色的)

但是当我在原始代码中颠倒“红色”和“绿色”时,它可以工作(蓝色圆圈中的区域应该是红色的),但我相信这是一种“创可贴”的方法

# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))

# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
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(breaks=c("Upper Third", "Middle Third","Lower Third"), values=c("red","orange","green"))

【问题讨论】:

    标签: r google-maps ggmap


    【解决方案1】:

    Percent_SEP12_Assets转为因子变量并指定级别的顺序:

    # create a new grouping variable
    Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 55, "Upper Third", "Middle Third"))
    Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
                                   levels = c("Upper Third", "Middle Third", "Lower Third"))
    
    
    # get the map
    bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
    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=5, alpha=0.6) +
      scale_color_manual(values=c("green","orange","red"))
    

    【讨论】:

    • 您的意思是在将Percent_SEP12_Assets 变成带有Percent_SEP12_Assets &lt;- ifelse(sep[,8] &lt;= 33, "Lower Third", ifelse(sep[,8] &gt;= 66, "Upper Third", "Middle Third")) 的组变量后将其转换为因子变量?
    • 是的,完全正确。我把答案改成了这样。
    • 仍然不工作 :-( 但是,如果我在我的个人代码中将“红色”与“绿色”切换,那么它可以工作。您的代码看起来正确,为什么它对我不起作用?跨度>
    • 显示变化,即绿色对应上三,但地图上的数据点不正确。将在原始帖子中发布详细信息
    • 对不起,我的错误,我没有测试代码,因为我没有数据。我编辑了我的答案。您需要在因子函数中指定级别。
    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2021-09-23
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多