使用ggplotly()时,ggplot图例消失了。

3

我试图保留ggplot生成的图例,但应用plotly后图例消失了。这是我的代码:

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")) 

地图world1数据的dput如下:
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")

我在下面附上一张图片,展示了这个情节在处理前的样子带图例的地球


请使用dput函数分享更多的数据,这样才能使问题可以复现。 - Quinten
我很惊讶这会创建一个ggplotplotly图。 你没有从scale_fill_gradientn(colors = ocean.curl(150))得到错误吗? 你应该得到错误。 你只有三个颜色组。 根据图例中的指示,这些不是数字; 这是一个字符字段。 您应该收到有关向连续比例提供离散值的错误。 尝试注释掉那一行。 对于最快速的答案,如@Quinten所提到的关于dput,请使您的问题可以再现。 看看这个:making R reproducible questions - Kat
抱歉,这里有一个链接,可以下载dput的.txt文件链接 - Conor
1个回答

3
guides(fill = guide_legend(title="")) 似乎是有问题的参数。删除它将把图例放回到 ggplotly 对象中。但是,图例标题仍然存在(我假设您希望删除该有问题的参数以删除图例标题)。此外,图例从因子转换为连续型。这似乎是与 ggplotly() 和图例 相关的持续问题之一,因此我不确定您的问题是否有一个干净的答案。
编辑:根据这篇文章,您可能需要在 ggplot() 外部构建自己的图例,并使用函数将该图例添加到 plotly() 中,然后使用 layout()
library(tidyverse)
library(plotly)
library(pals) # for ocean.curl()
options(scipen = 999999)

# https://dev59.com/KlwY5IYBdhLWcg3wATtt
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"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接