R:用不同颜色映射正负数
R: Mapping positive and negative numbers with different colors
我是一名记者,负责绘制 2002 年至 2012 年间黑人农民数量增加或减少的县的地图。我正在使用 R (3.2.3) 处理和绘制数据。
我已经能够用一种颜色绘制出县级收益和损失的整个范围——从负 40 到正 165——但这很难看出收益和损失的模式.我想做的是使单一颜色(比如蓝色)的所有变化都损失掉,并渲染第二种颜色(比如红色)的变化。
以下代码为出现正面和负面变化的县生成两个单独的(非常简化的)地图。任何人都知道如何在一张地图上以两种颜色捕获此信息?理想情况下,"Difference" 值为 0 的县将显示为灰色。感谢您观看!
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")
使用原始 post 中的源数据,这是我在上面评论中建议的使用 ggplot
的解决方案。
library(ggplot2)
library(ggmap)
library(maps)
library(dplyr)
# get data from
# https://quickstats.nass.usda.gov/results/A68E27D5-E9B2-3621-8F1E-58829A551F32
df <- read.csv("nass_data.csv")
df$County <- tolower(df$County)
df$State <- tolower(df$State)
#Calculate the difference between the 2002 and 2012 census95,
df <- df %>%
filter(Domain == "TOTAL", Year == 2002 | Year == 2012) %>%
group_by(County) %>%
mutate(Difference = ifelse(is.na(Value-lag(Value)), 0, Value-lag(Value))) %>%
select(County, State, Difference)
#get map data for US counties and states
county_map <- map_data("county")
county_map$County <- county_map$subregion
county_map$State <- county_map$region
#Join the data about farmers to the spatial data.
county_map <- left_join(county_map, df)
#plot using ggplot
ggplot(county_map, aes(x = long, y = lat, group=group)) +
geom_polygon(aes(fill = Difference)) +
scale_fill_gradient2(midpoint = 0, mid="#eee8d5", high="#dc322f", low="#268bd2")
我会注意到您的源数据似乎缺少全国的几个县。尽管如此,我认为这会让你得到你想要的。
将这些数据合并可能更好。仓位应该是什么,我快速判断了一下,你应该看看数据,看看是不是应该不一样。我还非常手动地进行了装箱,以尝试显示发生了什么。
使用 FIPS 代码("ANSI" 列的组合)可以在县名难以匹配的情况下提供帮助,因此我在这里这样做的原因。
人们往往会忽略 AK 和 HI,但那里似乎有一些农场。
此外,red/blue 是加载的颜色,确实应该避免。
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
我是一名记者,负责绘制 2002 年至 2012 年间黑人农民数量增加或减少的县的地图。我正在使用 R (3.2.3) 处理和绘制数据。
我已经能够用一种颜色绘制出县级收益和损失的整个范围——从负 40 到正 165——但这很难看出收益和损失的模式.我想做的是使单一颜色(比如蓝色)的所有变化都损失掉,并渲染第二种颜色(比如红色)的变化。
以下代码为出现正面和负面变化的县生成两个单独的(非常简化的)地图。任何人都知道如何在一张地图上以两种颜色捕获此信息?理想情况下,"Difference" 值为 0 的县将显示为灰色。感谢您观看!
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")
使用原始 post 中的源数据,这是我在上面评论中建议的使用 ggplot
的解决方案。
library(ggplot2)
library(ggmap)
library(maps)
library(dplyr)
# get data from
# https://quickstats.nass.usda.gov/results/A68E27D5-E9B2-3621-8F1E-58829A551F32
df <- read.csv("nass_data.csv")
df$County <- tolower(df$County)
df$State <- tolower(df$State)
#Calculate the difference between the 2002 and 2012 census95,
df <- df %>%
filter(Domain == "TOTAL", Year == 2002 | Year == 2012) %>%
group_by(County) %>%
mutate(Difference = ifelse(is.na(Value-lag(Value)), 0, Value-lag(Value))) %>%
select(County, State, Difference)
#get map data for US counties and states
county_map <- map_data("county")
county_map$County <- county_map$subregion
county_map$State <- county_map$region
#Join the data about farmers to the spatial data.
county_map <- left_join(county_map, df)
#plot using ggplot
ggplot(county_map, aes(x = long, y = lat, group=group)) +
geom_polygon(aes(fill = Difference)) +
scale_fill_gradient2(midpoint = 0, mid="#eee8d5", high="#dc322f", low="#268bd2")
将这些数据合并可能更好。仓位应该是什么,我快速判断了一下,你应该看看数据,看看是不是应该不一样。我还非常手动地进行了装箱,以尝试显示发生了什么。
使用 FIPS 代码("ANSI" 列的组合)可以在县名难以匹配的情况下提供帮助,因此我在这里这样做的原因。
人们往往会忽略 AK 和 HI,但那里似乎有一些农场。
此外,red/blue 是加载的颜色,确实应该避免。
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