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

问题描述 投票:2回答:1

这是我的数据:

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

但是当我将ggplot与参数frame一起使用时,出现错误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)
r ggplot2 plotly ggplotly
1个回答
0
投票

[如果其他人仍在寻找,这似乎是与geom_bar相关的错误。根据StéphaneLaurent的GitHub报告(https://github.com/ropensci/plotly/issues/1544),一种解决方法是使用geom_col(position = "dodge2")geom_col(position = "identity")而不是geom_bar(stat='identity')

© www.soinside.com 2019 - 2024. All rights reserved.