Reputation: 152
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.
Upvotes: 2
Views: 807
Reputation: 9886
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)
Upvotes: 1