I have the data.frame below and I have created a grouped bar chart. I'd like to edit the hover text by adding a new row after Department which will be named Department value and will take the column DemandCourse.x as its value for group Demand and AmountsAv.x for the group Amount Available.
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"), Department = c("Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry","Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry", "Chemsitry",
"Chemsitry", "Chemsitry", "Chemsitry"), DemandCourse.x = 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), DemandCourse.y = c(1115,
375, 1050, 300, 1739, 225, 800, 414, 414, 225, 450, 111, 675,
105, 224.5, 375, 75, 613, 601, 150, 1070, 364, 1519, 1095, 96.75,
250, 293, 25, 29500, 29500, 15, 500, 730, 25, 170, 240, 75, 1420,
534, 63, 613, 50, 144, 534, 1420, 63, 96.75, 152, 900, 613),
AmountsAv.x = 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), AmountsAv.y = c(0,
1000, 3000, 4000, 2000, 750, 4500, 2000, 5000, 150, 24000,
450, 3000, 1400, 1200, 400, 250, 1500, 5000, 1000, 22500,
12800, 2700, 0, 1500, 1500, 80000, 50, 8600, 8600, 200, 0,
3000, 500, 1200, 0, 1000, 0, 30000, 4500, 1500, 0, 200, 30000,
0, 4500, 1500, 400, 0, 1500)), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"), na.action = structure(293:294, .Names = c("293",
"294"), class = "omit"))
fig <- plot_ly(x = ~`Element Name`,text=~Department, data = merged3) %>%
add_bars(y = ~`DemandCourse.y`,
name = "Demand",
textposition = "none", # <--- added here
hovertemplate = paste0("Chemical Name: %{x}<br>",
"Total Demand: %{y}<br>",
"Department: %{text}<br><extra></extra>")) %>%
add_bars(y = ~`AmountsAv.y`,
name = "Amount Available",
textposition = "none", # <--- added here
hovertemplate = paste0("Chemical Name: %{x}<br>",
"Total Amount: %{y}<br>",
"Department: %{text}<br><extra></extra>")) %>%
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