R 3.2.1,在地图上反色 [英] R 3.2.1, reverse colors on map

查看:282
本文介绍了R 3.2.1,在地图上反色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是R代码的一部分

 #创建一个新的分组变量
Percent_SEP12_Assets< - ifelse(sep [,8] <= 33,Lower Third,ifelse(sep [,8]> = 55,Upper Third,Middle Third))

#获取地图
bbox< - make_bbox(sep $ Longitude,sep $ Latitude,f = 0.3)
map< - get_map(bbox)


#绘制地图和使用分组变量来填充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))

但不是地图制作

Upper Third与绿色
相对应中间三分之一与橙色
较低三分之一相对应红色

颜色方案混合在一起,即Upper Third与红色对应,而下三分之一与绿色对应。



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





我到目前为止所尝试的



以下代码

 #创建新的分组变量
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 ,下三分之一)


#获得地图
bbox< - make_bbox(sep $ Longitude,sep $ Latitude,f = 0.3)
map< ; - get_map(bbox)


#绘制地图并使用分组变量来填充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))

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

< img src =https://i.stack.imgur.com/OsLKW.jpgalt =

但是当我将red 和绿色在原来的代码,它的作品(在蓝色圆圈领域应该是红色),但我相信这是一个创可贴的方法

 #创建新的分组变量
Percent_SEP12_Assets< - ifelse(sep [,8] <= 33,Lower Third,ifelse(sep [,8]> = 55 ,Upper Third,Middle Third))

#获得地图
bbox< - make_bbox(sep $ Longitude,sep $ Latitude,f = 0.3)
map< - get_map(bbox)


#绘制地图并使用分组变量来填充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第三,下三分之一),值= c(红,橙,绿))

解决方案

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

 #创建一个新的分组变量
Percent_SEP12_Assets< - ifelse(sep [,8]< = 33,Lower Third,ifelse(sep [,8]> ; = 55,Upper Third,Middle Third))
Percent_SEP12_Assets < - 因子(Percent_SEP12_Assets,
levels = c(Upper Third,Middle Third,Lower Third)) )


#获取地图
bbox< - make_bbox(sep $ Longitude,sep $ Latitude,f = 0.3)
map< - get_map(bbox )


#绘制地图并使用分组变量来填充aes
ggmap(map)+
geom_point(data = sep,aes(x =经度,y =纬度,颜色= Percent_SEP12_Assets),大小= 5, alpha = 0.6)+
scale_color_manual(values = c(green,orange,red))


Here is part of R code

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

But instead of the map making

"Upper Third" correspond with Green "Middle Third" correspond with Orange "Lower Third" correspond with Red

The color scheme is mixed up, i.e. Upper Third corresponds with Red, and Lower Third corresponds with Green.

Higher numbers = good = green, but the map shows opposite. How to fix this?

What I tried so far

The following code

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

Will give this, which corrects the data labels, but the points in the map are inverted, i.e. where it is green it is red and vice versa (points in blue circle should be red)

But when I reversed "red" and "green" in original code, it works (area in blue circle supposed to be red), but I believe this is a "band-aid" approach

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

解决方案

Turn Percent_SEP12_Assets into a factor variable and specify the order of the levels:

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

这篇关于R 3.2.1,在地图上反色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆