簡體   English   中英

R:用不同的顏色映射正負數

[英]R: Mapping positive and negative numbers with different colors

我是一名記者,負責繪制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))

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

使用您原始帖子中的源數據,這是我上面的評論中建議的使用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,但似乎那里有一些農場。

另外,紅色/藍色是已加載的顏色,因此應避免使用。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM