Jeremy Bowyer
Jeremy Bowyer

Reputation: 152

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

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

Upvotes: 2

Views: 807

Answers (1)

MLavoie
MLavoie

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

Related Questions