I am trying to keep a legend which is generated when I use ggplot, but upon applying plotly the legend disappears. Here is my code:
ggplotchange <- ggplot(data = map.world1, aes(x = long, y = lat, group = group, fill = happiness, text = paste("Country:", region, "<br>", "Happiness:", -happiness, "<br>", "Economy:", economy, "<br>", "Family:", family, "<br>", "Health:", -health, "<br>", "Freedom:", -freedom, "<br>", "Trust:", trust, "<br>", "Generosity:", generosity))) +
geom_polygon() +
scale_fill_gradientn(colors = ocean.curl(150)) +
theme(
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5)) +
labs(title = "Change from 2015 to 2022") +
guides(fill = guide_legend(title=""))
ggplotly(ggplotchange, tooltip = c("text"))
The dput of the map.world1 data is this:
structure(list(long = c(-69.8991241455078, -69.8957061767578,
-69.9421920776367, -70.004150390625, -70.0661163330078, -70.0508804321289
), lat = c(12.4520015716553, 12.4229984283447, 12.4385251998901,
12.50048828125, 12.5469722747803, 12.5970697402954), group = c(1,
1, 1, 1, 1, 1), order = 1:6, region = c("Aruba", "Aruba", "Aruba",
"Aruba", "Aruba", "Aruba"), subregion = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_),
region.y = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), happiness = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), economy = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), family = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), health = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), freedom = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), trust = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), generosity = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), row.names = c(NA,
6L), class = "data.frame")
I've attached a photo of what the plot looks like before
guides(fill = guide_legend(title="")) seems to be the problematic argument. Removing it will put the legend back in the ggplotly object. However, the legend title is still there (which I assume you want removed since the problematic argument removes the legend title). Additionally, the legend is converted from factor to continuous. This seems to be one of the ongoing issues related to ggplotly() and legends, so I'm not sure if your question has a clean answer.
EDIT: Based on this post, you may have to build your own legend outside of ggplot() using functions and then add that legend to plotly() using layout().
library(tidyverse)
library(plotly)
library(pals) # for ocean.curl()
options(scipen = 999999)
# https://stackoverflow.com/questions/33135060/read-csv-file-hosted-on-google-drive
id <- "1yXECGkwlTjtcmeP2hqRzxFImYp5UbwpT" # google file ID
map.world1 <- dget(sprintf("https://docs.google.com/uc?id=%s&export=download", id))
ggplotchange <- ggplot(data = map.world1, aes(x = long, y = lat, group = group, fill = happiness, text = paste("Country:", region, "<br>", "Happiness:", -happiness, "<br>", "Economy:", economy, "<br>", "Family:", family, "<br>", "Health:", -health, "<br>", "Freedom:", -freedom, "<br>", "Trust:", trust, "<br>", "Generosity:", generosity))) +
geom_polygon() +
scale_fill_gradientn(colors = ocean.curl(150)) +
theme(
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5)) +
labs(title = "Change from 2015 to 2022", color = "")
ggplotly(ggplotchange, tooltip = c("text"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With