本文介绍了R绘制的动画图表仅显示在初始帧中具有数据的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的plotly创建动画气泡图,并且遇到了一个特别困难的问题.

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

由于美国前3年缺少值,因此即使在美国有数据的年份中,得出的图也根本不包含美国的点.

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.

结果图表的截屏

推荐答案

我不知道如何使用plotly对其进行修复,但是如果您对ggplotly没问题,那么似乎可以完成此任务:

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)

这篇关于R绘制的动画图表仅显示在初始帧中具有数据的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:48