R:绘制不同颜色的正数和负数 [英] R: Mapping positive and negative numbers with different colors

查看:241
本文介绍了R:绘制不同颜色的正数和负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名记者,负责绘制2002年至2012年期间黑人农民数量增加或减少的县。我正在使用R(3.2.3)来处理和映射数据。



我已经能够将单一颜色的县级收益和损失范围从负40到正165,很难看出盈亏模式。我想做的是使单一颜色(例如蓝色)的所有变化都会损失,并使第二种颜色(例如红色)的变化呈现增益。



以下代码为看到正负变化的县生成两个单独(非常简化的)地图。任何人都知道如何在单个地图上以两种颜色捕获这些信息?理想情况下,差值为0的县将显示为灰色。感谢您查看这个!

  df<  -  data.frame(GEOID = c(45001,22001 51001,21001,45003),
Difference = c(-10,-40,150,95,20))

#第二部分:构建一个shapefile加入。
县< - readOGR(dsn =Shapefile,layer =cb_2015_us_county_5m)

#将有关农民的数据与空间数据相关联。
counties @ data< - left_join(counties @ data,df)

在qtm方法中不允许#NAs,所以我们用0替换它们。
县$差异[is.na(县$差异)]< - 0

#这是失去黑人农民的县。
loss.counties< - 县[县#差] 0,]
qtm(loss.counties,差异)

#这些是获得黑人农民的县。
gain.counties< - 县[县#差]> 0,]
qtm(gain.counties,Difference)


解决方案

这个数据可能更好。我对这个垃圾箱应该做些什么,你应该看看数据是否应该是不同的。我也非常手动地进行了binning,试图展示发生了什么。



使用FIPS代码(ANSI列的组合)可以帮助在县名很难匹配,所以我为什么这样做。



人们倾向于放弃AK& HI,但似乎有一些农场。



此外,红/蓝色是加载颜色,真的应该避免。


$ b $
库(maptools)
库(rgeos)
库(albersusa)#
库devtools :: install_github(hrbrmstr / albersusa)
库(ggalt)
库(ggthemes)
库(dplyr)

df< - read.csv (347E31A8-7257-3AEE-86D3-4BE3D08982A3.csv)

df< - df%>%
过滤器(Domain ==TOTAL,Year == 2002 |年份== 2012)%>%
group_by(县)%>%
mutate(delta = Value-lag(Value),
delta = ifelse(is.na(delta) ,0,delta),
fips = sprintf(%02d%03d,State.ANSI,County.ANSI))

df $ delta< - cut(df $ delta, include.lowest = FALSE,
breaks = c(-400,-300,-200,-100,-1,1,100,200,300,400),
labels = c(301至400(损失),201至300,101至200,1至100,
/ $,+101至200,+201至300,301至400(增益)))

县; - counties_composite()
counties_map< - fortify(县,区域=fips)

gg< - ggplot()
gg< - gg + geom_map data = counties_map,map = counties_map,
aes(x = long,y = lat,map_id = id),
color =#b3b3b3,size = 0.15,fill =white)
gg< - gg + geom_map(data = df,map = counties_map,
aes(fill = delta,map_id = fips),
color =#b3b3b3,size = 0.15)
gg< - gg + scale_fill_manual(name =自2002以来更改\((白色=无数据)),
values = c(#543005,#8c510a,#bf812d #dfc27d,
#e0e0e0,
#80cdc1,#35978f,#01665e,#003c30),
guide = guide_legend ))
gg< - gg + coord_pr oj(us_laea_proj)
gg< - gg + labs(x =Gray == no data,y = NULL)
gg< - gg + theme_map()
gg& - gg + theme(legend.position = c(0.85,0.2))
gg< - gg + theme(legend.key = element_blank())
gg


I'm a journalist working to map the counties where the number of black farmers increased or decreased between 2002 and 2012. I am using R (3.2.3) to process and map the data.

I've been able to map the whole range of county-level gains and losses—which goes from negative 40 to positive 165—in a single color, but this makes it hard to see the pattern of gains and losses. What I'd like to do is make the losses all variations of a single color (say, blue), and render gains in variations of a second color (say, red).

The following code generates two separate (very simplified) maps for counties that saw positive and negative changes. Anyone know how to capture this information in two colors on a single map? Ideally, counties with a "Difference" value of 0 would appear in grey. Thanks for looking at this!

  df <- data.frame(GEOID = c("45001", "22001", "51001", "21001", "45003"), 
                        Difference = c(-10, -40, 150, 95, 20))

#Second part: built a shapefile and join.
counties <- readOGR(dsn="Shapefile", layer="cb_2015_us_county_5m")

#Join the data about farmers to the spatial data. 
counties@data <- left_join(counties@data, df)

#NAs are not permitted in qtm method, so let's replace them with zeros.  
counties$Difference[is.na(counties$Difference)] <- 0

#Here are the counties that lost black farmers.
loss.counties <- counties[counties$Difference < 0, ]
qtm(loss.counties, "Difference")

#Here are the counties that gained black farmers.
gain.counties <- counties[counties$Difference > 0, ]
qtm(gain.counties, "Difference")

解决方案

It's probably better to bin this data. I made a snap judgment for what the bins should be, you should look at the data to see if it should be different. I also did the binning very manually to try to show what's going on.

Using FIPS code (the combo of the "ANSI" columns) can help in situations where county names are hard to match, hence why I did that here.

Folks tend to leave out AK & HI but there are some farms there it seems.

Also, red/blue are loaded colors and really should be avoided.

library(ggplot2)
library(maps)
library(maptools)
library(rgeos)
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
library(ggalt)
library(ggthemes)
library(dplyr)

df <- read.csv("347E31A8-7257-3AEE-86D3-4BE3D08982A3.csv")

df <- df %>%
  filter(Domain == "TOTAL", Year == 2002 | Year == 2012) %>%
  group_by(County) %>%
  mutate(delta=Value-lag(Value),
         delta=ifelse(is.na(delta), 0, delta),
         fips=sprintf("%02d%03d", State.ANSI, County.ANSI)) 

df$delta <- cut(df$delta, include.lowest=FALSE,
                breaks=c(-400, -300, -200, -100, -1, 1, 100, 200, 300, 400),
                labels=c("301 to 400 (losses)", "201 to 300", "101 to 200", "1 to 100",
                         "no gains/losses", 
                         "+1 to 100", "+101 to 200", "+201 to 300", "301 to 400 (gains)"))

counties <- counties_composite()
counties_map <- fortify(counties, region="fips")

gg <- ggplot()
gg <- gg + geom_map(data=counties_map, map=counties_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#b3b3b3", size=0.15, fill="white")
gg <- gg + geom_map(data=df, map=counties_map,
                    aes(fill=delta, map_id=fips),
                    color="#b3b3b3", size=0.15)
gg <- gg + scale_fill_manual(name="Change since 2002\n(white = no data)",
                            values=c("#543005", "#8c510a", "#bf812d", "#dfc27d",
                                     "#e0e0e0",
                                     "#80cdc1", "#35978f", "#01665e", "#003c30"),
                            guide=guide_legend(reverse=TRUE))
gg <- gg + coord_proj(us_laea_proj)
gg <- gg + labs(x="Grey == no data", y=NULL)
gg <- gg + theme_map()
gg <- gg + theme(legend.position=c(0.85, 0.2))
gg <- gg + theme(legend.key=element_blank())
gg

这篇关于R:绘制不同颜色的正数和负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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