Reputation: 23
I'm trying to map an outcome measured at the county-level in Idaho. I'm not understanding the error message below. The following code reproduces the error. I've tried limiting map.data to just one row per county, but this also results in an error.
require(usmap)
require(ggplot2)
map.data <- usmap::us_map("counties",
include = "ID")
dim(map.data)
1090 10
random outcome values for plotting by county
n.county <- length(unique(map.data$fips))
set.seed(0)
d <- data.frame( fips = unique(map.data$fips),
values = runif(n.county))
head(d)
fips values
1 16001 0.8966972
2 16003 0.2655087
3 16005 0.3721239
4 16007 0.5728534
5 16009 0.9082078
6 16011 0.2016819
merge to add "values" variable to map.data
map.data <- merge( map.data, d)
map.data <- map.data[ , c("fips", "values")]
dim(map.data)
1090 2
plot_usmap( regions = "counties",
include = "ID",
data = map.data,
values = "values") +
labs( title = "Idaho",
subtitle = "Outcome Y")
R error:
This is the R error I get:
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (35884): fill
Next, I planned to color the counties based on the "values" using the following:
+ scale_fill_continuous( low = "white",
high = "red",
name = "Legend",
label = scales::comma) +
theme( legend.position = "right")
Upvotes: 2
Views: 398
Reputation: 8577
Apparently, the problem comes from the name of the second column of your dataframe, that you called "values". Maybe plot_usmap
breaks when the argument values
calls a column called values
.
Your code works when the name of that column is "X" for example:
require(usmap)
require(ggplot2)
map.data <- usmap::us_map("counties", include = "ID")
dim(map.data)
n.county <- length(unique(map.data$fips))
set.seed(0)
d <- data.frame( fips = unique(map.data$fips),
X = runif(n.county))
head(d)
map.data <- merge( map.data, d)
map.data <- map.data[ , c("fips", "X")]
dim(map.data)
1090 2
plot_usmap( regions = "counties",
include = "ID",
data = map.data,
values = "X") +
labs( title = "Idaho",
subtitle = "Outcome Y")
Upvotes: 1