gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:36:34 25 4
gpt4 key购买 nike

这是我的数据:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com