I am creating a population pyramid in highcharter and the way I have got it to display as a pyramid is to change the value for males to be a negative value.
I have successfully changed the labels on the x axis to be absolute values (all positive), however the tooltip is still showing the negative value. Can someone help here please? Code and image below
Thanks
PyramidPlot2 <- highchart() %>%
hc_add_series(PopEDBinded, type = "bar", hcaes(x = c(PyramidLevels,PyramidLevels), y = value, group = Gender), color = c("#2f7ed8","#FF0000"),
showInLegend = F) %>%
hc_plotOptions(bar = list(stacking = "normal")) %>%
# format the labels on the x-axis (y-axis per HC)
hc_yAxis(labels = list(formatter = htmlwidgets::JS(
"function() {
return Math.abs(this.value); /* all labels to absolute values */
}"
))) %>%
hc_xAxis(categories = PyramidLevels, reversed = F)%>%hc_title(text = "Males", align = "center", x = -115, y = 20, margin = 0,
style = list(fontSize = "12px", color = "#FF0000")) %>%
hc_subtitle(text = "Females", align = "center", y = 20, margin = 0,
x = 250, style = list(fontSize = "12px", color = "#2f7ed8"))
I am not sure how to add data but here is the output of dput on the dataframe in question:
structure(list(Sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Males",
"Females", "Both Sexes"), class = "factor"), Age = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L), levels = c("Age 0-4", "Age 5-9",
"Age 10-14", "Age 15-19", "Age 20-24", "Age 25-29", "Age 30-34",
"Age 35-39", "Age 40-44", "Age 45-49", "Age 50-54", "Age 55-59",
"Age 60-64", "Age 65-69", "Age 70-74", "Age 75-79", "Age 80-84",
"Age 85 and over"), class = "factor"), CSO.Electoral.Divisions.2022 = c("Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County",
"Doonloughan, Galway County", "Doonloughan, Galway County"),
Census.Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "2022", class = "factor"),
Statistic = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "Population", class = "factor"),
value = c(1, 4, 2, 4, 4, 2, 2, 4, 2, 4, 3, 2, 3, 4, 3, 2,
1, 1, -3, -3, -3, -3, -1, -1, -4, -5, -2, -2, -2, -1, -6,
-6, 0, -4, -2, -1), Percentage = c(2.1, 8.3, 4.2, 8.3, 8.3,
4.2, 4.2, 8.3, 4.2, 8.3, 6.2, 4.2, 6.2, 8.3, 6.2, 4.2, 2.1,
2.1, 6.1, 6.1, 6.1, 6.1, 2, 2, 8.2, 10.2, 4.1, 4.1, 4.1,
2, 12.2, 12.2, 0, 8.2, 4.1, 2), Gender = c("Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Female", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male")), row.names = c(NA,
-36L), class = "data.frame")
