Reputation: 165
I am using the minicharts package (https://www.rdocumentation.org/packages/leaflet.minicharts/versions/0.6.2) in a leaflet map of mine. It works as expected, however, I wish I could adjust the pop up and legend titles and values. You can see my leaflet map below:
I would like the values in the pop up to have % sign after. This would require turning this value into a string. The values, however, have to remain as doubles for the minicharts to read the value and produce the pie chart. Another option would be to set a title for the popup, ex. Sequence % per variant. This would work as well. It would also be handy, if the variants would only show in the popup if they were greater than 0. Lastly, I would also like to set a legend title for the legend on the right side, which shows what colors of the pie chart correspond to which variants.
The code is below:
final_variant <- final_variant %>%
addMinicharts(
JointLabel$long, JointLabel$lat,
type = "pie",
chartdata = JointLabel[, c("Alpha_B.1.1.7",
"B.1.1.7.E484",
"Beta_B.1.351",
"Gamma_P.1",
"Omicron_B.1.1.529",
"B.1.617",
"Kappa_B.1.617.1",
"Delta_B.1.617.2",
"Mu_B.1.621",
"other"
)],
colorPalette = colors,
popupOptions = list(closeButton = FALSE, showTitle = TRUE),
width = 30
)
final_variant is just a map file from the spatial file of Europe. I can provide this too, if needed.
Upvotes: 2
Views: 649
Reputation: 33
Without your original dataset I can't help with specifics, but this workflow has worked for me in the past. Basically, you create a string separate from your dataset with all of the HTML styling you want, and call that string in the popup argument in addMiniCharts. This prevents modifying the data being used to generate the map.
library(leaflet)
library(leaflet.minicharts)
library(tidyverse)
set.seed(123)
JointLabel <- tibble(
point = 1:5,
x = runif(5, min = -80, max = -70),
y = runif(5, min = 40, max = 50),
total_obs = sample(50:100, 5, replace = TRUE),
Alpha = runif(5, 10, 50),
Beta = runif(5, 5, 30),
Omicron = runif(5, 1, 20)
)
cols <- names(JointLabel)[-(1:4)]
JointLabel <- JointLabel %>%
mutate(across(all_of(cols), ~ (.x / total_obs) * 100))
generate_tooltips <- function(data) {
data %>%
pivot_longer(cols = all_of(cols), names_to = "label", values_to = "percentage") %>%
filter(percentage > 0) %>%
group_by(point) %>%
summarise(
popup = paste0(label, ": ", round(percentage, 1), "%",
collapse = "<br>"
),
.groups = "drop"
) %>%
group_by(point) %>%
summarise(popup = first(popup)) %>%
pull(popup)
}
tips_24 <- generate_tooltips(JointLabel)
chartdata <- JointLabel %>%
select(c(-x, -y, -point, -total_obs))
final_variant <- leaflet(data = JointLabel) %>%
addTiles() %>%
addProviderTiles("Esri.WorldImagery") %>%
addMinicharts(
lat = JointLabel$y,
lng = JointLabel$x,
chartdata = chartdata,
type = "pie",
popup = popupArgs(html = tips_24)
)
final_variant
This solution is a little hacky but it works well! The generate_tooltips
function assumes the data is in a wide format with lat, long, and a column for each variable in the minicharts.
Upvotes: 0