情节R:如何添加属于框架的动态注释

发布于 2025-01-27 11:35:30 字数 1757 浏览 4 评论 0原文

我在一项简单的任务上挣扎。我有一个包含3列的数据库:

  • 年龄(数字)
  • pop(
  • 数字)
  • PORP60:年龄> = 60的个体百分比(如60:12%的poeple'的字符串类似于')。此值的每行相同。 数据集看起来像:

“在此处输入图像描述”

我建立了一个基于年份的框架的绘图。因此,我有一个滑块,可以让我在每个年龄段显示个人数量,这是一年一度的动画。

我想添加一种阳极源,以显示帧年度部分60的值...我知道将GGPLOT发送到GGPLOTLY函数是可能的(对我来说)更容易控制和遵循我的代码的逻辑。

这是我的代码:

gH <- plot_ly(data = dataH,
              name = 'Hommes',
              marker = list(color = ispfPalette[4]),
              x = ~Pop,
              y = ~Age,
              frame = ~Annee)
gH <- gH %>% layout(yaxis = list(categoryorder="array",
                                 categoryarray=dataH$Age))
gH <- gH %>% layout(yaxis = list(title = '',
                                 zeroline = TRUE,
                                 showline = TRUE,
                                 showticklabels = TRUE,
                                 showgrid = FALSE), 
                    xaxis = list(title = '',
                                 zeroline = TRUE,
                                 showline = TRUE,
                                 autorange = "reversed"),
                    shapes = hline(60)) 
gH <- gH %>% add_annotations(
  x = 3000,
  y = 62,
  text = 'Part des 60 ans et + : 12 %',
  showarrow = F,
  color = ispfPalette[8]

其中 text ='部分des 60 ans et +:12%'应该由我替换我可以获得属于属于的值的东西滑块的一年。

有人可以帮我做吗?

事先感谢您的大力帮助。

I'm struggeling on a simple task. I have a database with 3 columns :

  • Year (numeric)
  • Age (numeric)
  • Pop (numeric)
  • Part60 : The % of individuals with age >= 60 (string like '% of poeple over 60 : 12%'). This value is the same for each rows of a year.
    Dataset looks like :

enter image description here

I built a plotly bargraph with a frame based on the year. So I have a slider which allow me to show for each age the number of individuals and this is animated year by year.

I would like to add an anotation which shows the value of Part60 for the year of the frame... I know that it's possible with a ggplot sent to ggplotly function, however I want to do it from scratch with a plot_ly function as parameters are (for me) easier to control and follow the logic of my code.

enter image description here

This is my code :

gH <- plot_ly(data = dataH,
              name = 'Hommes',
              marker = list(color = ispfPalette[4]),
              x = ~Pop,
              y = ~Age,
              frame = ~Annee)
gH <- gH %>% layout(yaxis = list(categoryorder="array",
                                 categoryarray=dataH$Age))
gH <- gH %>% layout(yaxis = list(title = '',
                                 zeroline = TRUE,
                                 showline = TRUE,
                                 showticklabels = TRUE,
                                 showgrid = FALSE), 
                    xaxis = list(title = '',
                                 zeroline = TRUE,
                                 showline = TRUE,
                                 autorange = "reversed"),
                    shapes = hline(60)) 
gH <- gH %>% add_annotations(
  x = 3000,
  y = 62,
  text = 'Part des 60 ans et + : 12 %',
  showarrow = F,
  color = ispfPalette[8]

Where text = 'Part des 60 ans et + : 12 %' should be replaced by something which allow me to get the value which belongs to the year of the slider.

Is someone may help me to do it ?

Thanks in advance for your great help.

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

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

发布评论

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

评论(1

南薇 2025-02-03 11:35:30

由于我没有您的数据,因此很难给您最好的答案。虽然,这是一种可以在整个动画中添加更改文本的方法。

library(plotly)
library(tidyverse)

data(gapminder, package = "gapminder")
str(gapminder)

funModeling::df_status(gapminder)
# continent, lifeExp, year

gap <- gapminder %>% group_by(year, continent) %>%
  summarise(Expectancy = mean(lifeExp))

# plot 
p1 <- plot_ly(gap, x = ~Expectancy, y = ~continent,
              frame = ~year, type = 'bar', 
              showlegend = F,
              hovertemplate = paste0("Continent: %{y}<br>",
                                     "<extra></extra>"),
              texttemplate = "Life Expectancy: %{x:.2f}") %>%
  layout(yaxis=list(title=""), 
         xaxis=list(title="Average Life Expectancy per Continent By Year"),
         title=list(text=paste("Fancy Title")),
         margin = list(t = 100))
p1

如果您有文本,则需要动画,该文字未连接到每个标记(bar,point,line),那么您可以这样做。

# Something to add in the annotation text
gap2 <- gap %>% filter(continent == "Asia") %>% 
  droplevels() %>% 
  arrange(year)

# build to see frames
p2 <- plotly_build(p1)

# modify frames; need an annotation for each frame
# make sure the data is in order by year (order by frame)
lapply(1:nrow(gap2), # for each frame
       function(i){
         annotation = list(
           data = gap2,
           type = "text",
           x = 77,
           y = .5,
           yref = "paper",
           showarrow = F,
           text = paste0("Asian Life Expectancy<br>", 
                         sprintf("%.2f", gap2[i, ]$Expectancy)), 
           font = list(color = "#b21e29", size = 16))
         p2$x$frames[[i]]$layout <<- list(annotations = list(annotation)) # change plot
       })
p2

如果有什么不清楚的话,请告诉我。

Since I don't have your data, it's pretty difficult to give you the best answer. Although, here is a method in which you can add text that changes throughout the animation.

library(plotly)
library(tidyverse)

data(gapminder, package = "gapminder")
str(gapminder)

funModeling::df_status(gapminder)
# continent, lifeExp, year

gap <- gapminder %>% group_by(year, continent) %>%
  summarise(Expectancy = mean(lifeExp))

# plot 
p1 <- plot_ly(gap, x = ~Expectancy, y = ~continent,
              frame = ~year, type = 'bar', 
              showlegend = F,
              hovertemplate = paste0("Continent: %{y}<br>",
                                     "<extra></extra>"),
              texttemplate = "Life Expectancy: %{x:.2f}") %>%
  layout(yaxis=list(title=""), 
         xaxis=list(title="Average Life Expectancy per Continent By Year"),
         title=list(text=paste("Fancy Title")),
         margin = list(t = 100))
p1

enter image description here

If you had text you wanted to animate that is not connected to each marker (bar, point, line), then you could do it this way.

# Something to add in the annotation text
gap2 <- gap %>% filter(continent == "Asia") %>% 
  droplevels() %>% 
  arrange(year)

# build to see frames
p2 <- plotly_build(p1)

# modify frames; need an annotation for each frame
# make sure the data is in order by year (order by frame)
lapply(1:nrow(gap2), # for each frame
       function(i){
         annotation = list(
           data = gap2,
           type = "text",
           x = 77,
           y = .5,
           yref = "paper",
           showarrow = F,
           text = paste0("Asian Life Expectancy<br>", 
                         sprintf("%.2f", gap2[i, ]$Expectancy)), 
           font = list(color = "#b21e29", size = 16))
         p2$x$frames[[i]]$layout <<- list(annotations = list(annotation)) # change plot
       })
p2

enter image description here

If anything is unclear, let me know.

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