I'm trying to plot a gradient fill map of all counties in the United States using the following code. I'd like to have a continuous darkblue -> darkred gradient fill for the map fill, but for some reason my code isn't doing this even though I added:
scale_colour_gradientn("Title", colours = c("darkblue", "darkred"),
values = c(0,1))
to the plot. How do I make a gradual gradient from darkblue to darkred in this case (data scale is from 0 to 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())
You've set your fill
to a value and color
to NA in this line geom_sf(aes(fill=per_gop), color=NA)
. I suspect this means you should be using scale_fill_gradientn
not scale_color_gradientn
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.