在 R 中,使用 geom_sf + frame 并将其传递给 plotly::ggplotly 会给我转换错误,我错过了什么?
In R, using geom_sf + frame and passing it to plotly::ggplotly gives me transitions error, what did I miss?
我正在尝试制作一个等值统计图,其中包含季度失业数据的过渡(帧)。 ggplot2 地图接缝很好,但 plotly 中帧之间的过渡不是。它们应该只改变颜色(基于新季度该状态值的变化),但渲染每一帧的过程是合并状态(按颜色)或类似的东西。
我找不到解决方案。关于此行为和可能的解决方案的任何线索?
我(主要)遵循这个很好的演练:https://moderndata.plot.ly/learning-from-and-improving-upon-ggplotly-conversions/
使用 sf 0.8-1 ; plotly 4.9.0 , ggplot2 3.2.0
我的代码:
library(sf) # m shapefiles
library(ggplot2) # graphics
library(plotly) # dynamic graphics
library(dplyr) # data-wrangling tidyverse
library(geobr) # download shapesfiles from Brazil (ibge)
library(sidrar) # download data (sidra)
library(rmapshaper) # data-wrangling shapefiles
library(lubridate) # for dates
# download shapefiles from Brazilian States (UF) - IPEA geobr::
ufs <- geobr::read_state(code_state="all", year=2018)
# simplify the shapes rmapshaper::ms_simplify
ufs <- rmapshaper::ms_simplify(ufs)
# download data from SIDRA - unemployment by state X quarter X gender
d <-sidrar::get_sidra(api = "/t/6396/n3/all/v/4099/p/all/c2/all/d/v4099%201")
# renaming columns
d <- d %>% dplyr::mutate(date = `Trimestre (Código)`,
UF = `Unidade da Federação (Código)`)
# dates as dates
d$date <-lubridate::yq(d$date)
# selecting some quarters and only the total of both genders
d1 <- d %>% dplyr::filter(Sexo == "Total" , date <= "2013-01-01") %>%
dplyr::select(UF, date, Valor)
d1$UF <- as.numeric(d1$UF)
# Joining geodata with unemployment rate, by = "uf"
ufs2 <- dplyr::full_join(ufs, d1, by = c("code_state" = "UF" ))
# plot map
m0<-ggplot2::ggplot(ufs2) +
geom_sf(mapping = aes(fill = Valor, frame = date))
# map plotly::
m <- plotly::ggplotly(m0) %>%
style(hoverlabel = list(bgcolor = "white"), hoveron = "fill")
m
目前有问题的结果:
非常感谢!
我认为,如果您按照该博客 post 中的做法从框架中删除 x/y 数据,这可能会奏效,例如:
gg <- p %>%
ggplotly() %>%
style(hoverlabel = list(bgcolor = "white"), hoveron = "fill") %>%
plotly_build()
# remove x/y data from every trace
gg$x$frames <- lapply(
gg$x$frames, function(f) {
f$data <- lapply(f$data, function(d) d[!names(d) %in% c("x", "y")])
f
})
gg
我正在尝试制作一个等值统计图,其中包含季度失业数据的过渡(帧)。 ggplot2 地图接缝很好,但 plotly 中帧之间的过渡不是。它们应该只改变颜色(基于新季度该状态值的变化),但渲染每一帧的过程是合并状态(按颜色)或类似的东西。
我找不到解决方案。关于此行为和可能的解决方案的任何线索?
我(主要)遵循这个很好的演练:https://moderndata.plot.ly/learning-from-and-improving-upon-ggplotly-conversions/
使用 sf 0.8-1 ; plotly 4.9.0 , ggplot2 3.2.0
我的代码:
library(sf) # m shapefiles
library(ggplot2) # graphics
library(plotly) # dynamic graphics
library(dplyr) # data-wrangling tidyverse
library(geobr) # download shapesfiles from Brazil (ibge)
library(sidrar) # download data (sidra)
library(rmapshaper) # data-wrangling shapefiles
library(lubridate) # for dates
# download shapefiles from Brazilian States (UF) - IPEA geobr::
ufs <- geobr::read_state(code_state="all", year=2018)
# simplify the shapes rmapshaper::ms_simplify
ufs <- rmapshaper::ms_simplify(ufs)
# download data from SIDRA - unemployment by state X quarter X gender
d <-sidrar::get_sidra(api = "/t/6396/n3/all/v/4099/p/all/c2/all/d/v4099%201")
# renaming columns
d <- d %>% dplyr::mutate(date = `Trimestre (Código)`,
UF = `Unidade da Federação (Código)`)
# dates as dates
d$date <-lubridate::yq(d$date)
# selecting some quarters and only the total of both genders
d1 <- d %>% dplyr::filter(Sexo == "Total" , date <= "2013-01-01") %>%
dplyr::select(UF, date, Valor)
d1$UF <- as.numeric(d1$UF)
# Joining geodata with unemployment rate, by = "uf"
ufs2 <- dplyr::full_join(ufs, d1, by = c("code_state" = "UF" ))
# plot map
m0<-ggplot2::ggplot(ufs2) +
geom_sf(mapping = aes(fill = Valor, frame = date))
# map plotly::
m <- plotly::ggplotly(m0) %>%
style(hoverlabel = list(bgcolor = "white"), hoveron = "fill")
m
目前有问题的结果:
非常感谢!
我认为,如果您按照该博客 post 中的做法从框架中删除 x/y 数据,这可能会奏效,例如:
gg <- p %>%
ggplotly() %>%
style(hoverlabel = list(bgcolor = "white"), hoveron = "fill") %>%
plotly_build()
# remove x/y data from every trace
gg$x$frames <- lapply(
gg$x$frames, function(f) {
f$data <- lapply(f$data, function(d) d[!names(d) %in% c("x", "y")])
f
})
gg