添加一个下拉菜单到使用循环创建的Plotly柱状图。

huangapple go评论94阅读模式
英文:

Add a dropdown menu to plotly created bar chart using a loop

问题

我想使用Plotly创建一个条形图,x轴显示年龄组,y轴显示比例。然后,我想要一个下拉菜单,通过它可以根据地区更新图形。我的实际数据有20个地区,所以最好使用循环创建菜单的函数。类似于这个

  1. Mdf <- data.frame(age=c(15,20,25,30,35,40,45),cases=c(1200,1110,2233,1445,1323,1510,910),py=c(10101,12465,452424,14240,1544,1574,1154))
  2. Mdf <- rbind(Mdf,Mdf)
  3. Mdf$cases[8:14] <- Mdf$cases[1:7]+3
  4. Mdf$prop <- (Mdf$cases / Mdf$py)*100
  5. Mdf$reg <- c(rep(1,7),rep(2,7))
  6. library(tidyverse)
  7. library(plotly)
  8. p <-
  9. plot_ly(
  10. y = Mdf$prop,
  11. x = Mdf$age,
  12. type = "bar"
  13. )

我会很感激您的帮助。

英文:

I want to create a bar graph with plotly that shows the age group on the x axis and the prop on the y axis. Then I want to have a dropdown mwenu that update the figure by reg. My actual data has 20 regions so it will be great to a function with a loop that create the menu automatically. Something similar to this

  1. Mdf &lt;- Mdf &lt;- data.frame(age=c(15,20,25,30,35,40,45),cases=c(1200,1110,2233,1445,1323,1510,910),py=c(10101,12465,452424,14240,1544,1574,1154))
  2. Mdf &lt;- rbind(Mdf,Mdf)
  3. Mdf$cases[8:14] &lt;- Mdf$cases[1:7]+3
  4. Mdf$prop &lt;- (Mdf$cases / Mdf$py)*100
  5. Mdf$reg &lt;- c(rep(1,7),rep(2,7))
  6. library(tidyverse)
  7. library(plotly)
  8. p &lt;-
  9. plot_ly(
  10. y = Mdf$prop,
  11. x = Mdf$age,
  12. type = &quot;bar&quot;
  13. )

I would appreciate your help

答案1

得分: 1

使用您在链接中提供的函数,您将得到以下结果:

  1. Mdf <- data.frame(age=c(15,20,25,30,35,40,45),cases=c(1200,1110,2233,1445,1323,1510,910),py=c(10101,12465,452424,14240,1544,1574,1154))
  2. Mdf <- rbind(Mdf,Mdf)
  3. Mdf$cases[8:14] <- Mdf$cases[1:7]+553
  4. Mdf$prop <- (Mdf$cases / Mdf$py)*100
  5. Mdf$reg <- c(rep("a",7),rep("b",7))
  6. library(tidyverse)
  7. library(plotly)
  8. p <- Mdf %>%
  9. plot_ly(
  10. y = ~prop,
  11. x = ~age,
  12. text = ~reg,
  13. type = "bar",
  14. hoverinfo = 'text',
  15. transforms = list(
  16. list(
  17. type = 'filter',
  18. target = ~reg,
  19. operation = '=',
  20. value = unique(Mdf$reg)[1]
  21. )
  22. )) %>% layout(
  23. updatemenus = get_menu_list(unique(Mdf$reg))
  24. )

希望这有所帮助。如果您需要进一步的解释或帮助,请告诉我。

英文:

Using the function you provided in the link, you get the following

  1. Mdf &lt;- Mdf &lt;- data.frame(age=c(15,20,25,30,35,40,45),cases=c(1200,1110,2233,1445,1323,1510,910),py=c(10101,12465,452424,14240,1544,1574,1154))
  2. Mdf &lt;- rbind(Mdf,Mdf)
  3. Mdf$cases[8:14] &lt;- Mdf$cases[1:7]+553
  4. Mdf$prop &lt;- (Mdf$cases / Mdf$py)*100
  5. Mdf$reg &lt;- c(rep(&quot;a&quot;,7),rep(&quot;b&quot;,7))
  6. library(tidyverse)
  7. library(plotly)
  8. p &lt;- Mdf %&gt;%
  9. plot_ly(
  10. y = ~prop,
  11. x = ~age,
  12. text = ~reg,
  13. type = &quot;bar&quot;,
  14. hoverinfo = &#39;text&#39;,
  15. transforms = list(
  16. list(
  17. type = &#39;filter&#39;,
  18. target = ~reg,
  19. operation = &#39;=&#39;,
  20. value = unique(Mdf$reg)[1]
  21. )
  22. )) %&gt;% layout(
  23. updatemenus = get_menu_list(unique(Mdf$reg))
  24. )

huangapple
  • 本文由 发表于 2023年2月27日 16:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定