将条形图所有组成部分的总计值添加到悬停文本

发布于 2025-01-14 00:19:04 字数 4500 浏览 2 评论 0原文

我有下面的数据框:

dp<-structure(list(`Element Name` = c("Naphthalene", "Nitric acid (concentrated)", 
"Sulphuric acid(concentrated)", "2-hydroxybenzoic acid", "Acetic anhydride", 
"2-Naphthol", "Sodium Hydroxide", "Phenyl hydrazine hydrochloride", 
"Glucose", "Sodium acetate", "Aniline", "Zinc poweder", "2-amino-benzoic acid", 
"1.3-dihydroxybenzene", "Ethyl acetate", "hydroxy benzene", "phenyl methanol", 
"Sodium carbonate", "Potassium permanganate", "Sodium bisulfite.", 
"Hydrochloric acid (concentrated)", "Sodium nitrite", "Copper(II) sulfate", 
"Sodium chloride.", "Methyl orange", "EtOH", "Distilled water", 
"cuper ion", "ammonium hydroxide", "ammonium hydroxide", "Iron( III)", 
"Nitric Acid", "Potassium Thiocyanate", "ferric ammonium sulfate", 
"Ammonium Sulfate", "Sodium hydroxide", "sodium hypochlorite", 
"Hydrochloric acid", "Acetic acid", "Phenolphthalein", "Sodium carbonate", 
"Sodium Acetate", "Sodum hydroxide", "Acetic acid", "Hydrochloric acid", 
"Phenolphthalein", "Methyl orange", "Phosphoric acid", "Amonium hydroxide", 
"Sodium carbonate", "Iron(II) sulfate", "Potassium permanganate", 
"Sulfuric Acid", "Barium Chloride.monoHydrate", "Distilled water", 
"nickel Sulphate", "Dimethyl glyoxime (DMG)", "Calsium chloride", 
"Calsium Carbonate", "Ammonium oxalate", "potassium chromate", 
"Lead (II) Nitrate", "   Ammonium thiocyanate10%", "Copper(II) sulfate", 
"Nitric acid", "    Iron (II)", "Ammonium Nitrate", "Chloroform", 
"Iodine solution", "sodiumThiosulphates", "Copper(II) sulfate", 
"Amonia solution", "murexide", "Lead (II) Nitrate", "ammonium chloride", 
"sodium diethyl dicabamate", "Eriochrome Black T", "nikel sulphate", 
"Ethanol", "Dimethyl glyoxime (DMG)", "Methyl orange", "Sodium bicarbonate", 
"Calcium chloride", "Tartaric acid", "Iron(II) sulfate", "Hydrogen peroxide", 
"Sodium Hydroxide", "Sulfuric Acid", "Citric acid", "Benzoic acid", 
"Methanol", "Ethanol", "Iron(III) Chloride", "Phenol", "Phthalic acid", 
"Salicylic acid", "Ammonium oxalate", "Hydrochloric acid (concentrated)", 
"Ammonium tartarate", "Ammonium citrate"), DemandCourse = c(240, 
375, 1050, 300, 1614, 225, 75, 414, 414, 225, 450, 111, 675, 
105, 120, 375, 75, 75, 375, 150, 750, 264, 975, 900, 20, 250, 
30, 25, 2500, 2500, 15, 500, 730, 25, 170, 240, 75, 750, 255, 
10, 160, 50, 144, 54, 630, 15, 18, 132, 900, 48, 138, 36, 300, 
2250, 45, 1500, 90, 999, 15, 750, 120, 996, 300, 480, 234, 30, 
30, 20, 80, 64, 64, 500, 24, 24, 212, 24, 20, 200, 20, 32, 20, 
100, 100, 60, 100, 100, 200, 200, 60, 60, 240, 240, 100, 200, 
60, 60, 60, 200, 60, 60), AmountsAv = c(NA, 1000, 3000, 4000, 
1000, 750, 750, 2000, 5000, 150, 24000, 450, 3000, 1400, 400, 
400, 250, 250, 1000, 1000, 7500, 6400, 900, NA, 250, 1500, 20000, 
50, 300, 4000, 200, NA, 3000, 500, 1200, NA, 1000, NA, 6000, 
900, 250, NA, 200, 6000, NA, 900, 250, 200, NA, 250, 150, 1000, 
15000, 3000, 20000, 1500, 600, 7500, 20, 500, 300, 1600, 400, 
900, 300, NA, 10, 12000, 200, 20, 900, 400, 180, 1600, 180, 20, 
750, 200, 5000, 600, 250, 3000, 1800, 1500, 150, 250, 750, 15000, 
150, 250, 200, 5000, 400, 150, 300, 1500, 500, 7500, 300, 400
)), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(293:294, .Names = c("293", "294"), class = "omit"))

amd 我创建了分组条形图,但正如您在屏幕截图中看到的,虽然条形的高度正确,但悬停文本中的值不同。我猜发生这种情况是因为有多个元素具有某些情况下名称相同。我需要在悬停文本中添加一个 Total 值。 输入图片此处描述

fig <- plot_ly(x = ~`Element Name`, data = dp) %>% 
  add_bars(y = ~`DemandCourse`, 
           name = "Demand", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>", 
                                  "Demand: %{y}<br>"
                                  )) %>% 
  add_bars(y = ~AmountsAv,
           name = "Amount Available", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>",  
                                  "Available Amount: %{y}<br>"
                                  )) %>% 
  layout(barmode = "group",              # <--- dropped showLegend (doesn't go here)
         xaxis = list(title = "Element Name", tickangle=45),
         yaxis = list(title = "Amount Available"),
         title = "Amount and Demand per Element")
fig

I have the dataframe below:

dp<-structure(list(`Element Name` = c("Naphthalene", "Nitric acid (concentrated)", 
"Sulphuric acid(concentrated)", "2-hydroxybenzoic acid", "Acetic anhydride", 
"2-Naphthol", "Sodium Hydroxide", "Phenyl hydrazine hydrochloride", 
"Glucose", "Sodium acetate", "Aniline", "Zinc poweder", "2-amino-benzoic acid", 
"1.3-dihydroxybenzene", "Ethyl acetate", "hydroxy benzene", "phenyl methanol", 
"Sodium carbonate", "Potassium permanganate", "Sodium bisulfite.", 
"Hydrochloric acid (concentrated)", "Sodium nitrite", "Copper(II) sulfate", 
"Sodium chloride.", "Methyl orange", "EtOH", "Distilled water", 
"cuper ion", "ammonium hydroxide", "ammonium hydroxide", "Iron( III)", 
"Nitric Acid", "Potassium Thiocyanate", "ferric ammonium sulfate", 
"Ammonium Sulfate", "Sodium hydroxide", "sodium hypochlorite", 
"Hydrochloric acid", "Acetic acid", "Phenolphthalein", "Sodium carbonate", 
"Sodium Acetate", "Sodum hydroxide", "Acetic acid", "Hydrochloric acid", 
"Phenolphthalein", "Methyl orange", "Phosphoric acid", "Amonium hydroxide", 
"Sodium carbonate", "Iron(II) sulfate", "Potassium permanganate", 
"Sulfuric Acid", "Barium Chloride.monoHydrate", "Distilled water", 
"nickel Sulphate", "Dimethyl glyoxime (DMG)", "Calsium chloride", 
"Calsium Carbonate", "Ammonium oxalate", "potassium chromate", 
"Lead (II) Nitrate", "   Ammonium thiocyanate10%", "Copper(II) sulfate", 
"Nitric acid", "    Iron (II)", "Ammonium Nitrate", "Chloroform", 
"Iodine solution", "sodiumThiosulphates", "Copper(II) sulfate", 
"Amonia solution", "murexide", "Lead (II) Nitrate", "ammonium chloride", 
"sodium diethyl dicabamate", "Eriochrome Black T", "nikel sulphate", 
"Ethanol", "Dimethyl glyoxime (DMG)", "Methyl orange", "Sodium bicarbonate", 
"Calcium chloride", "Tartaric acid", "Iron(II) sulfate", "Hydrogen peroxide", 
"Sodium Hydroxide", "Sulfuric Acid", "Citric acid", "Benzoic acid", 
"Methanol", "Ethanol", "Iron(III) Chloride", "Phenol", "Phthalic acid", 
"Salicylic acid", "Ammonium oxalate", "Hydrochloric acid (concentrated)", 
"Ammonium tartarate", "Ammonium citrate"), DemandCourse = c(240, 
375, 1050, 300, 1614, 225, 75, 414, 414, 225, 450, 111, 675, 
105, 120, 375, 75, 75, 375, 150, 750, 264, 975, 900, 20, 250, 
30, 25, 2500, 2500, 15, 500, 730, 25, 170, 240, 75, 750, 255, 
10, 160, 50, 144, 54, 630, 15, 18, 132, 900, 48, 138, 36, 300, 
2250, 45, 1500, 90, 999, 15, 750, 120, 996, 300, 480, 234, 30, 
30, 20, 80, 64, 64, 500, 24, 24, 212, 24, 20, 200, 20, 32, 20, 
100, 100, 60, 100, 100, 200, 200, 60, 60, 240, 240, 100, 200, 
60, 60, 60, 200, 60, 60), AmountsAv = c(NA, 1000, 3000, 4000, 
1000, 750, 750, 2000, 5000, 150, 24000, 450, 3000, 1400, 400, 
400, 250, 250, 1000, 1000, 7500, 6400, 900, NA, 250, 1500, 20000, 
50, 300, 4000, 200, NA, 3000, 500, 1200, NA, 1000, NA, 6000, 
900, 250, NA, 200, 6000, NA, 900, 250, 200, NA, 250, 150, 1000, 
15000, 3000, 20000, 1500, 600, 7500, 20, 500, 300, 1600, 400, 
900, 300, NA, 10, 12000, 200, 20, 900, 400, 180, 1600, 180, 20, 
750, 200, 5000, 600, 250, 3000, 1800, 1500, 150, 250, 750, 15000, 
150, 250, 200, 5000, 400, 150, 300, 1500, 500, 7500, 300, 400
)), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(293:294, .Names = c("293", "294"), class = "omit"))

amd I create the grouped bar chart but as you can see in the screenshot while the height of the bars is correct the value in the hover text is different.I guess this happens because there are more than one elements with same name for some cases.What I need is to add a Total value in the hover text.
enter image description here

fig <- plot_ly(x = ~`Element Name`, data = dp) %>% 
  add_bars(y = ~`DemandCourse`, 
           name = "Demand", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>", 
                                  "Demand: %{y}<br>"
                                  )) %>% 
  add_bars(y = ~AmountsAv,
           name = "Amount Available", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>",  
                                  "Available Amount: %{y}<br>"
                                  )) %>% 
  layout(barmode = "group",              # <--- dropped showLegend (doesn't go here)
         xaxis = list(title = "Element Name", tickangle=45),
         yaxis = list(title = "Amount Available"),
         title = "Amount and Demand per Element")
fig

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

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

发布评论

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

评论(1

你又不是我 2025-01-21 00:19:04

一种选择是在绘图之前聚合数据:

library(plotly)
library(dplyr)

dp <- dp %>% 
  group_by(`Element Name`) %>% 
  summarise(across(everything(), sum, na.rm = TRUE))

fig <- plot_ly(x = ~`Element Name`, data = dp) %>% 
  add_bars(y = ~`DemandCourse`, 
           name = "Demand", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>", 
                                  "Demand: %{y}<br>"
           )) %>% 
  add_bars(y = ~AmountsAv,
           name = "Amount Available", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>",  
                                  "Available Amount: %{y}<br>"
           )) %>% 
  layout(barmode = "group",              # <--- dropped showLegend (doesn't go here)
         xaxis = list(title = "Element Name", tickangle=45),
         yaxis = list(title = "Amount Available"),
         title = "Amount and Demand per Element")
fig

在此处输入图像描述

One option would be to aggregate your data before plotting:

library(plotly)
library(dplyr)

dp <- dp %>% 
  group_by(`Element Name`) %>% 
  summarise(across(everything(), sum, na.rm = TRUE))

fig <- plot_ly(x = ~`Element Name`, data = dp) %>% 
  add_bars(y = ~`DemandCourse`, 
           name = "Demand", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>", 
                                  "Demand: %{y}<br>"
           )) %>% 
  add_bars(y = ~AmountsAv,
           name = "Amount Available", 
           textposition = "none",          # <--- added here
           hovertemplate = paste0("Chemical Name: %{x}<br>",  
                                  "Available Amount: %{y}<br>"
           )) %>% 
  layout(barmode = "group",              # <--- dropped showLegend (doesn't go here)
         xaxis = list(title = "Element Name", tickangle=45),
         yaxis = list(title = "Amount Available"),
         title = "Amount and Demand per Element")
fig

enter image description here

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