情节R:如何添加属于框架的动态注释
我在一项简单的任务上挣扎。我有一个包含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 :
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我没有您的数据,因此很难给您最好的答案。虽然,这是一种可以在整个动画中添加更改文本的方法。
如果您有文本,则需要动画,该文字未连接到每个标记(bar,point,line),那么您可以这样做。
如果有什么不清楚的话,请告诉我。
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.
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.
If anything is unclear, let me know.