英文:
error in R plot_usmap() when trying to color counties by outcome variable
问题
I'm here to provide the translated code part:
我试图映射在爱达荷州县级测量的结果。我不理解下面的错误消息。以下代码重现了错误。我尝试限制map.data仅为每个县选择一行,但这也会导致错误。
require(usmap)
require(ggplot2)
map.data <- usmap::us_map("counties",
include = "ID")
dim(map.data)
1090 10
# 用于按县绘制的随机结果值
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
# 合并以添加"values"变量到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错误:
这是我收到的R错误:
> 不知道如何自动选择数据框类型的比例。默认为连续型。
错误:美学必须要么是长度为1,要么与数据相同(35884):fill
接下来,我计划根据"values"对县进行着色,使用以下方式:
+ scale_fill_continuous( low = "white",
high = "red",
name = "Legend",
label = scales::comma) +
theme( legend.position = "right")
英文:
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")
答案1
得分: 1
问题似乎出在你的数据框的第二列名称上,你将其命名为"values"。也许当参数"values"调用名为"values"的列时,plot_usmap
会出现问题。
你的代码在该列的名称为"X"时可以正常工作,例如:
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")
请注意,我只提供了代码的翻译部分。
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论