3

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 beforeworld with legend

Conor
  • 63
  • 3
  • Could you please share more data using `dput`? Now it is not reproducible – Quinten Jun 29 '22 at 17:33
  • I'm surprised this would create a `ggplot` or `plotly` plot. You aren't getting an error from `scale_fill_gradientn(colors = ocean.curl(150))`? You should be. You've only got three color groups. From how they're indicated in the legend, these are not numbers; it's a character field. You should be getting an error regarding discrete values supplied to a continuous scale. Try commenting out that line. For the best answers quickly, as @Quinten mentioned regarding `dput`, make your question reproducible. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Jun 30 '22 at 02:14
  • Sorry, here is a link to the dput as a .txt file [link](https://drive.google.com/file/d/1yXECGkwlTjtcmeP2hqRzxFImYp5UbwpT/view?usp=sharing) – Conor Jun 30 '22 at 12:03

1 Answers1

3

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"))
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30