使用frame参数从ggplot绘图制作Plotly图表

3

这是我的数据:

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出现错误:Error in -data$group : invalid argument to unary operator

以下是我的ggplot代码:

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

你能否修改我的ggplot代码以制作相同的图表?

该示例使用frame参数成功运行:

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

我收到了“警告:忽略未知的美学元素:frame”。这是什么意思? - Humpelstielzchen
@Humpelstielzchen 这个 frame 参数是用于 Plotly 的,而不是 ggplot2。 - Tao Hu
那是一个bug,我认为。我刚在Github上开了一个问题来报告它。 - Stéphane Laurent
1个回答

3

如果其他人仍在寻找答案,这似乎是与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')


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接