R Plotly animated chart only showing groups with data in initial frame

老子叫甜甜 提交于 2019-12-11 13:47:02

问题


I'm trying to create an animated bubble chart using plotly in R, and I've run into a particularly difficult issue.

library(plotly)
dates <- 2000:2010
countries <- c("US", "GB", "JP")
df <- merge(dates, countries, all=TRUE)
names(df) <- c("Date", "Country")


df$x <- rnorm(nrow(df))
df$y <- rnorm(nrow(df))

df[1:3, c("x", "y")] <- NA

p <- plot_ly(df, x=~x, y=~y, color=~Country, frame=~Date, type="scatter", mode="markers")
p

Due to the missing values for the US' first 3 years, the resulting plot doesn't include the US' dot at all, even for the years where the US has data.

Screenshot of resulting chart


回答1:


I don't know how to fix it with plotly but if you are ok with ggplotly it seems to do the job:

p <- ggplot(df, aes(x, y, color = Country)) +
  geom_point(aes(frame = Date)) + theme_bw()

ggplotly(p)


来源:https://stackoverflow.com/questions/50843134/r-plotly-animated-chart-only-showing-groups-with-data-in-initial-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!