我正试图制作一张合唱图,其中包含季度失业数据的转换图()。ggplot2映射缝得很好,但帧之间的转换却不是很好。它们应该只改变颜色(根据新季度状态值的变化),但是呈现每个帧的过程是合并状态(按颜色)或类似的东西。
我没能找到解决办法。关于这种行为和可能的解决方案有什么线索吗?
我遵循(大部分)这个很好的演练:https://moderndata.plot.ly/learning-from-and-improving-upon-ggplotly-conversions/
使用SF0.8-1;巧妙地使用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到目前为止有问题的结果:

事先非常感谢!
发布于 2019-09-19 14:39:35
我认为,如果您像在博客文章中所做的那样,从框架中删除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
})
gghttps://stackoverflow.com/questions/58003600
复制相似问题