error in R plot_usmap() when trying to color counties by outcome variable

huangapple go评论79阅读模式
英文:

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 &lt;- usmap::us_map(&quot;counties&quot;,
                          include = &quot;ID&quot;)
dim(map.data)
1090   10

random outcome values for plotting by county

n.county &lt;- length(unique(map.data$fips))
set.seed(0)
d &lt;- 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 &lt;- merge( map.data, d)
map.data &lt;- map.data[ , c(&quot;fips&quot;, &quot;values&quot;)]
dim(map.data)
1090   2

plot_usmap( regions = &quot;counties&quot;,
            include = &quot;ID&quot;, 
            data = map.data,
            values = &quot;values&quot;) + 
  labs( title = &quot;Idaho&quot;,
        subtitle = &quot;Outcome Y&quot;) 

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 = &quot;white&quot;, 
                           high = &quot;red&quot;, 
                           name = &quot;Legend&quot;, 
                           label = scales::comma) + 
  theme( legend.position = &quot;right&quot;)

答案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 &lt;- usmap::us_map(&quot;counties&quot;, include = &quot;ID&quot;)
dim(map.data)
n.county &lt;- length(unique(map.data$fips))
set.seed(0)
d &lt;- data.frame( fips = unique(map.data$fips),
                 X = runif(n.county))
head(d)

map.data &lt;- merge( map.data, d)
map.data &lt;- map.data[ , c(&quot;fips&quot;, &quot;X&quot;)]
dim(map.data)
1090   2

plot_usmap( regions = &quot;counties&quot;,
            include = &quot;ID&quot;, 
            data = map.data,
            values = &quot;X&quot;) + 
  labs( title = &quot;Idaho&quot;,
        subtitle = &quot;Outcome Y&quot;) 

huangapple
  • 本文由 发表于 2020年1月3日 22:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定