r - 使用 frame 参数绘制从 ggplot 到 plotly 的图

标签 r ggplot2 plotly ggplotly

这是我的数据:

data <- data.table(year = rep(1980:1985,each = 5),
                   Relationship = rep(c(" Acquaintance","Unknown","Wife","Stranger","Girlfriend","Friend"),  5),
                   N = sample(1:100, 30)
                   )

我可以使用 plotly::plot_ly 函数来绘制这样的年份动态图:

plot_ly(data
        ,x=~Relationship
        ,y=~N
        ,frame=~year
        ,type = 'bar'
)

enter image description here

但是当我使用带参数 frame 的 ggplot 时,出现错误 -data$group 中的错误:一元运算符的参数无效

这是我的 ggplot 代码:

p <- ggplot(data = data,aes(x =Relationship,y = N ))+
  geom_bar(stat = "identity",aes(frame = year))
ggplotly(p)

你能修改我的 ggplot 代码来生成相同的图形吗?

此示例使用框架参数成功运行:

data(gapminder, package = "gapminder")
gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year)) +
  scale_x_log10()
ggplotly(gg)

最佳答案

以防其他人仍在寻找,这确实是与 geom_bar 相关的错误。根据 Stéphane Laurent 的 GitHub 报告 (https://github.com/ropensci/plotly/issues/1544),解决方法是使用 geom_col(position = "dodge2")geom_col(position = "identity") 而不是 geom_bar(stat='identity')

关于r - 使用 frame 参数绘制从 ggplot 到 plotly 的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56490613/

相关文章:

删除 ggplot2 中 geom_ribbon 的图例键周围的填充

javascript - 使用 plotly.js 将 modeBar 放在顶部中心

python - 为什么 R 的 read.csv() 可以从 GitLab URL 读取 CSV 而 pandas 的 read_csv() 不能?

R - 逗号分隔的列表之间是否至少有一个匹配项?

r - 如何更改方面标签?

r - dplyr 和 ggplot 管道未按预期工作

python - 使用 Dash Cytoscape 在回调中更改节点的标签

python - 在 GitHub Pages 上显示交互式绘图图表(.html 文件)

R:显示多边形几何特征列表

r - 使 udpipe_annotate() 更快