Animating r plotly

Visualising metagenomic data collected over time

Ingrid Aulike

See the Metagenomic Data tab for a description of this data.

Animating ggplotly, and r plotly generally, only works on scatter points, including geom_segment. That’s why in this animation, rather than a stacked bar chart, I have thick line segments connecting the cumulative relative abundances. This chart with thick line segments look the same as a stacked bar chart on my laptop, but on some devices the thickness of the lines will look wrong.

Hovering tooltips don’t work with segments - I didn’t want tooltips in this animation anyway.

I made the animation option “transition = 0” because with any sort of positive transition time, some of the line segments come flying in from the right! I would have liked to see all the abundances smoothly change as the animation plays, but I’ll have to think of how to get that. The problem arises when the abundances of some phyla are 0 at one time and not zero at the next. Also when “transition \(\gt\) 0”, segments that were very small but not zero at one time point, that become bigger at the next time point, begin the transition as thin, black lines and evolve to the thick, coloured state, which looks weird. I have in mind how I might do this by fixing the start of every line segment at y=0 and drawing the cumulative abundance y values over the top of one another in decreasing order.

Animating is unofficial in ggplotly; I got the error message “Ignoring unknown aesthetics: frame, ids” but it worked anyway.

g <- ggplot(pFPhyla, 
  aes(x=Baby, xend=Baby, y=abundstart, yend=abundend, colour=Bacteria)) +
  geom_segment(stat="identity", size=4, aes(frame= Month, ids=Bacteria)) + 
  theme(axis.text.x = element_text(angle=30,hjust=1,vjust=1)) + 
  ylab("Abundance") + 
  scale_color_futurama()

ggplotly(g,tooltip="") %>% animation_opts(800, mode="afterall",transition=0)

I quite like this animation. If nothing else, you can see that the babies had a large relative abundance of proteobacteria to begin with but later there is a predominance of actinobacteria.