这是我的数据:
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'
)
但是当我将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)
答案 0 :(得分: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')