【问题标题】:Error in match.arg(regions) : 'arg' must be NULL or a character vectormatch.arg(regions) 中的错误:“arg”必须为 NULL 或字符向量
【发布时间】:2020-02-20 23:39:58
【问题描述】:

我想在 r 上制作一张地图,并通过颜色的强度来显示价格弹性。这是我使用线性回归得到的数据:

    IN  log_PRICE   -1.1059770  0.05943414  -18.60845   7.186435e-72
    KY  log_PRICE   -1.0459502  0.03410250  -30.67078   2.739798e-196
    OH  log_PRICE   -0.9076732  0.01259117  -72.08806   0.000000e+00
    TX  log_PRICE   -0.2252409  0.01053847  -21.37321   4.117490e-101
    IN  log_PRICE   -1.1059770  0.05943414  -18.60845   7.186435e-72
    KY  log_PRICE   -1.0459502  0.03410250  -30.67078   2.739798e-196
    OH  log_PRICE   -0.9076732  0.01259117  -72.08806   0.000000e+00
    TX  log_PRICE   -0.2252409  0.01053847  -21.37321   4.117490e-101

library(usmap)
library(ggplot2)
library(tmpa)
library(sf)
library(leaflet)

plot_usmap(regions="state", data = CC1, values = "estimate", 
    include = c("IN", "KY", "OH", "TX"), color = "orange") + 
  scale_fill_continuous(low = "white", high = "orange", 
     name = "Price elasticity", label = scales::comma) + 
  labs(title = "Price elasticity for Cold Cereal", 
       subtitle = "States include: IN, KY, OH, TX") +
  theme(legend.position = "right") 

#That is the code I wrote but I keep getting this error
Error in match.arg(regions) : 'arg' must be NULL or a character vector

【问题讨论】:

  • 我有一个问题。例如,在您的数据中,您有两个俄亥俄州的数据点。当你填充俄亥俄多边形时,你需要哪个数字?
  • 我需要 log_price 的积分;出于某种原因,我在代码上复制并粘贴了两次我需要的相同数据,但它是我想要使用的前 4 个。

标签: r usmap


【解决方案1】:

我不确定是什么导致了错误。尽管该函数可能需要字符向量,但似乎使用了非字符向量。您想在您的代码和我的代码之间进行比较。也许,您也想检查数据。以下对我有用。

plot_usmap(data = mydata, values = "estimate",
           regions = "state", include = c("IN", "KY", "OH", "TX")) +
scale_fill_continuous(low = "white", high = "orange", 
                      name = "Price elasticity", label = scales::comma) + 
labs(title = "Price elasticity for Cold Cereal", 
     subtitle = "States include: IN, KY, OH, TX") +
theme(legend.position = "right")

为了好玩,以下是我自己的带有albersusa包的版本。

library(tydyverse)
library(sf)
library(albersusa)
library(viridis)

mysf <- left_join(usa_sf("laea"), mydata, by = c("iso_3166_2" = "state")) 

ggplot() +
geom_sf(data = mysf, aes(fill = estimate)) +
scale_fill_viridis(discrete = FALSE, option = "plasma")

数据

mydata <- structure(list(state = c("IN", "KY", "OH", "TX"), log = c("log_PRICE", 
"log_PRICE", "log_PRICE", "log_PRICE"), estimate = c(-1.105977, 
-1.0459502, -0.9076732, -0.2252409), se = c(0.05943414, 0.0341025, 
0.01259117, 0.01053847), statistic = c(-18.60845, -30.67078, 
-72.08806, -21.37321), pvalue = c(7.186435e-72, 2.739798e-196, 
0, 4.11749e-101)), class = "data.frame", row.names = c(NA, -4L
))

【讨论】:

    猜你喜欢
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多