【问题标题】:R - Getting correct two color gradient on ggplot2 mapR - 在 ggplot2 地图上获得正确的两种颜色渐变
【发布时间】:2018-02-22 01:05:33
【问题描述】:

我正在尝试使用以下代码绘制美国所有县的渐变填充图。我想为地图填充使用连续的深蓝色->深红色渐变填充,但由于某种原因,即使我添加了代码,我的代码也没有这样做:

scale_colour_gradientn("Title", colours = c("darkblue", "darkred"),
                        values = c(0,1))

到剧情。在这种情况下,如何使从深蓝色到深红色的渐变渐变(数据刻度从 0 到 1)?

counties_dem_rep <- read.csv("2016_US_County_Level_Presidential_Results.csv", header = TRUE, sep = ",")

library(ggplot2)
library(fiftystater)
library(colorplaner)
library(USAboundaries)
library(sf)
library(dplyr)

usc <- us_counties(resolution = c("low", "high"), states = NULL)

temp_count_NM <- us_counties(states = "New Mexico", resolution ='high')

st_list = c("Alabama", "Arizona", "Arkansas", "California", "Colorado", 
            "Connecticut", "Delaware", "Florida", "Georgia", "Idaho", "Illinois", 
            "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", 
            "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", 
            "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New 
            Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", 
            "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
            "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West 
            Virginia", "Wisconsin", "Wyoming")

plot_counties <- us_counties(states=st_list, resolution = 'high')
plot_counties$geoid <- as.numeric(plot_counties$geoid)
colnames(counties_dem_rep)[11] <- "geoid"

plot_counties <- inner_join(plot_counties, counties_dem_rep, by = "geoid")

plot_states <- us_states(states=st_list, resolution = 'high')

temp_count_NM$geoid <- as.numeric(temp_count_NM$geoid)

temp_count_NM <- inner_join(temp_count_NM, counties_dem_rep, by = "geoid")



devtools::install_github("tidyverse/ggplot2")
require(ggplot2)

ggplot(plot_counties) + 
  geom_sf(aes(fill=per_gop), color=NA) + 
  geom_sf(data=plot_states, fill=NA, color="black", lwd=0.25) + 
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  scale_colour_gradientn("Title", colours = c("darkblue", "darkred"),
                        values = c(0,1))+ 
  theme(panel.background = element_blank(), axis.ticks = element_blank(), 
        axis.text = element_blank())

目前我得到以下情节:

【问题讨论】:

  • scale_fill_gradientn(...)?

标签: r ggplot2 maps


【解决方案1】:

geom_sf(aes(fill=per_gop), color=NA) 这一行中,您已将fill 设置为一个值,并将color 设置为NA。我怀疑这意味着你应该使用scale_fill_gradientn 而不是scale_color_gradientn

【讨论】:

  • 你是对的。在这种情况下,我也建议使用 coord_map()。
猜你喜欢
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多