R - ggplotly 在加入列后抛出错误

问题描述 投票:0回答:2

我目前正在开展一个项目,该项目可以可视化来自美国的交通站点数据。为此,我将德克萨斯州和加利福尼亚州某些年份的交通站点数量可视化。我创建了一张带有标签的分区统计图,效果很好。我从 R 中的

geo_data
包中获取了
maps
sf。由于标签太多,我想使用
plotly
包和
ggplotly()
函数创建悬停标签。 但是,如果我尝试使用 ggplotly 绘制我的等值线地图,我会收到一条错误消息:
the number of columns of matrices must match
。这是我使用的代码:

p1 <- df %>%
  group_by(county_fips)%>%
  count()%>%
  full_join(geo_data, by = c("county_fips" = "fips")) %>%
  st_as_sf() %>%
  ggplot(aes(fill = n))+
  geom_sf()+
  geom_sf_text(aes(label = ID), fun.geometry = st_centroid)+
  scale_fill_continuous(low = "antiquewhite2", high = "palevioletred4", guide = "colorbar")+
  theme_void()
ggplotly(p1)

这是 df 的示例:

id    state stop_date  county_name county_fips
<int> <fct> <date>     <fct>     <int>
 1      CA    2013-01-01 San Diego      6073
 2      CA    2013-01-01 San Diego      6073
 3      CA    2013-01-01 San Diego      6073
 4      CA    2013-01-01 San Diego      6073
 5      CA    2013-01-01 NA             NA
 6      CA    2013-01-01 Orange         6059
 7      CA    2013-01-01 Orange         6059
 8      CA    2013-01-01 Orange         6059
 9      CA    2013-01-01 Orange         6059

geo_data sf 已使用此代码和包创建

maps
:

sf_map <- st_as_sf(map("county", plot = F, fill = T))
sf_map <- sf_map %>% filter(str_detect(ID, "california") | str_detect(ID, "texas"))
sf_map <- sf_map %>% filter(ID != "missouri,texas" & ID != "oklahoma,texas")
sf_map$ID <- gsub("texas,galveston", "texas,galveston:main", sf_map$ID)
data("county.fips")
geo_data <- left_join(sf_map, county.fips, by = c("ID" = "polyname"))

我的假设是,这与以下事实有关:我拥有所有县的 geo_data,但并非每个县都停止。这会在连接中的

county_fips
参数中创建缺失值。我尝试排除数据中在计数前丢失county_fips的情况,但错误保持不变。

这是加入后我的数据示例:

county_fips      n ID                                                                    geometry
         <int>  <int> <chr>                                                       <MULTIPOLYGON [°]>
 1        6001 724809 california,alam~ (((-121.4785 37.4829, -121.5129 37.4829, -121.8853 37.4829, ~
 2        6003  37749 california,alpi~ (((-120.0748 38.70903, -120.0518 38.72049, -119.9544 38.7777~
 3        6005  32375 california,amad~ (((-120.0748 38.70903, -120.069 38.51995, -120.1263 38.5085,~
 4        6007  89359 california,butte (((-121.6217 39.31063, -121.9082 39.29345, -121.9082 39.3335~

我希望有人能告诉我在哪里查看代码和数据以找到并解决问题。 提前非常感谢!

r ggplot2 plotly data-visualization choropleth
2个回答
1
投票

我并不声称我的修复对您有用,但我希望它能提供一些想法。对我来说,我已经使用

ms_simplify()
进行了转换,并且我发现添加参数
explode=TRUE
对我的情况有所帮助。

# Read datasets
facilities.lines.df.raw = read.csv(facilities.lines.path)
facilities.df.raw = read.csv(facilities.path)
facilities.shp = read_sf(facilities.shp.path)
districts.shp = read_sf(districts.shp.path)

# Cleaning
facilities.df = ...  # left this out
facilities.lines.df = ...  # left this out

# Scaling / projection system
districts.shp.trans <- st_transform(
  districts.shp, 4326)
# Reduce num of polys
districts.shp.trans.1 <- ms_simplify(
  districts.shp.trans, 
  keep=0.01,
  explode=TRUE)  # <----------- Adding "explode=TRUE" fixed my issue

# Linestrings
facility.linestrings = ...  # left this out
facility.multilinestring = st_multilinestring(
  do.call("rbind", facility.linestrings))
facility.multilinestring.st_sfc = st_sfc(
  facility.multilinestring, crs=PLANAR_XFORM_SCALAR_x2)

# Plot
gg = ggplot(districts.shp.trans.1) +
  geom_sf() +
  geom_sf(
    data=facility.multilinestring.st_sfc) + 
  geom_point(
    data=facilities.df,
    aes(x=longitude, y=latitude)))
ggplotly(gg)

0
投票

这可能是由于位置代码的 stop 数据没有几何数据 --- 例如,

NA
第 5 行
county_fips
中的
df
--- 而不是另一种方式。 (对于遇到此问题的其他人来说,最好检查所有数据源是否同意在将县级 FIPS 代码表示为字符串时是否对县级 FIPS 代码进行零填充。)这会导致连接将空条目引入
 geometry
,这会导致
ggplotly()
出现问题。

避免此错误的一种方法就是过滤掉这些有问题的行:

  [...] %>%
  filter(!st_is_empty(geometry)) %>%
  ggplot([...]) [...]

但请注意,这也意味着不绘制部分停止数据。最好调查一下为什么 NA 和任何其他不匹配的 FIPS 代码会进入您的停止数据,并解决那里的任何潜在问题和/或准备更全面的地理数据对象。

附加信息:

  • 空的几何图形可以用不同的方式表达和打印;在尝试了各种不同的不成功的连接之后,我看到它打印为“GEOMETRYCOLLECTION EMPTY”、“MULTIPOLYGON EMPTY”或“EMPTY”,具有不同的
    geometry
    列类和不同的数据帧类,具体取决于事物的构造方式。因此,使用如上所述的
    st_is_empty()
    之类的方法可能很重要,以确保检测到这里的所有可能性。
  • 当混合存在空和非空几何条目时,它似乎会给出这种形式的错误:
Error in (function (..., deparse.level = 1)  : 
  number of columns of matrices must match (see arg <some number>)

如果所有几何条目都是空的,则似乎会给出这种形式的错误:

Error in `calc_limits_bbox()` at ggplot2/R/coord-sf.R:190:5:
! Scale limits cannot be mapped onto spatial coordinates in `coord_sf()`
ℹ Consider setting `lims_method = "geometry_bbox"` or `default_crs = NULL`.
Run `rlang::last_trace()` to see where the error occurred.
© www.soinside.com 2019 - 2024. All rights reserved.