在动画情节散射图中,如何更改每个框架的轴刻度...但是在R? (适应Python解决方案吗?)

发布于 2025-01-22 02:13:13 字数 1818 浏览 3 评论 0原文

我正在尝试在R中制作动画绘图图,其中动画期间轴的最小值和最大变化。最近有人向Python提出了这个确切的问题,并通过使用布局更新并通过“框架”来解决。

python解决方案在这里:是否有一种方法可以动态更改每个帧的绘图动画轴刻度?

这是Python代码。

import plotly.express as px
df = px.data.gapminder()
df = df[(df['continent'] == 'Asia') & (df['year'].isin([1997, 2002, 2007]))]

scales = [2002]

fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

yranges = {2002:[0, 200]}

for f in fig.frames:
    if int(f.name) in yranges.keys():
        f.layout.update(yaxis_range = yranges[int(f.name)])

fig.show()

我一直在查看R示例和R绘图对象,但我不知道...如何将此解决方案应用于R?

这里有一个解决方案在这里使用Shiny,但是动画的光滑得多,没有滑块,我对Shinny一无所知,所以如果可以的话,我宁愿避免使用: https://community.plotly.com/t/what----------------- performant-way-way-totate-a-a-graph-with-new-with-new-data/639/6?u=fabern

如果它很有用,我写了我的代码来开始在r中重新创建示例...尽我所能。也许我需要使用Plotly Expressly Express或Dash之类的?

library("gapminder")
library('data.table')
  data(package = "gapminder")
  df <- as.data.table(as.data.frame(gapminder))
  df <- df[continent=='Asia' & year %in% c(1997, 2002, 2007)]
  
  graph_animated <- plot_ly(df, x = ~gdpPercap, y = ~lifeExp, frame = ~year, size = ~pop,
                            type = 'scatter', color = ~continent)

I'm trying to make an animated plotly graph within R where both axes' min and max change during the animation. Someone recently asked this exact question for python, and it was solved by using layout update and looping through the "frames."

Python solution here: Is there a way to dynamically change a plotly animation axis scale per frame?

Here is the python code.

import plotly.express as px
df = px.data.gapminder()
df = df[(df['continent'] == 'Asia') & (df['year'].isin([1997, 2002, 2007]))]

scales = [2002]

fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

yranges = {2002:[0, 200]}

for f in fig.frames:
    if int(f.name) in yranges.keys():
        f.layout.update(yaxis_range = yranges[int(f.name)])

fig.show()

I've been looking through the R examples and the R plotly object, and I can't figure out... how would I apply this solution to R?

There's a solution using Shiny here, but the animation is much less smooth, it doesn't have the slider, and I don't know anything about shiny, so I'd rather avoid it if I could: https://community.plotly.com/t/what-is-the-most-performant-way-to-update-a-graph-with-new-data/639/6?u=fabern

In case it's useful, I wrote up my code to start recreating the example in R... as far as I could get.Maybe I need to be using plotly express or dash or some such?

library("gapminder")
library('data.table')
  data(package = "gapminder")
  df <- as.data.table(as.data.frame(gapminder))
  df <- df[continent=='Asia' & year %in% c(1997, 2002, 2007)]
  
  graph_animated <- plot_ly(df, x = ~gdpPercap, y = ~lifeExp, frame = ~year, size = ~pop,
                            type = 'scatter', color = ~continent)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2025-01-29 02:13:13

最简单的方法是构建图,然后添加更改。您必须先构建,以查看要修改的帧。

# build
plt <- plotly_build(graph_animated)

# modify the layout for the second frame
plt$x$frames[[2]]$layout = list(yaxis = list(range = c(0, 200)))

之后,您可以像其他任何情节一样对其进行可视化。

The easiest way to do this is to build the plot and then add the change. You have to build first, in order to see the frames you want to modify.

# build
plt <- plotly_build(graph_animated)

# modify the layout for the second frame
plt$x$frames[[2]]$layout = list(yaxis = list(range = c(0, 200)))

After that, you can visualize it as you would any other plot.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文