繁体   English   中英

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

[英]R - ggplotly throws error after joining columns

我目前正在从事一个可视化美国交通站点数据的项目。 为此,我可视化了某些年份德克萨斯州和加利福尼亚州的交通站点数量。 我创建了一个带有标签的等值线图,效果很好。 我从 R 中的maps包中获取了geo_data 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 by-argument 中创建缺失值。 我试图在我的数据中排除计数前缺少 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~

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

我并不是说我的修复对你有用,但我希望它能提供一些想法。 对我来说,我使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM