向悬停文本添加条形图所有组件的总值

Add to hover text a Total value of all the components of a bar

我有以下数据框:

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 我创建了分组条形图,但正如您在屏幕截图中看到的那样,条形的高度是正确的,悬停文本中的值是 different.I 我猜这是因为有多个元素与某些 cases.What 同名 我需要的是在悬停文本中添加一个 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

一种选择是在绘图之前汇总您的数据:

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