【问题标题】:R Plotly animated chart only showing groups with data in initial frameR Plotly动画图表仅显示初始帧中具有数据的组
【发布时间】:2018-06-13 17:34:27
【问题描述】:

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

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 年的值缺失,因此结果图根本不包括美国的点,即使对于美国有数据的年份也是如此。

Screenshot of resulting chart

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    我不知道如何使用 plotly 修复它,但如果你对 ggplotly 没问题,它似乎可以完成这项工作:

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

    【讨论】:

    • 这看起来可以解决问题,至少在我提供的示例代码中是这样。我会将它应用到我的实际项目中,看看是否可行。非常感谢。
    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2012-09-18
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多