简体   繁体   English

使用 ggplotly() 时 ggplot 图例消失

[英]ggplot legend disappears when using ggplotly()

I am trying to keep a legend which is generated when I use ggplot, but upon applying plotly the legend disappears.我试图保留使用 ggplot 时生成的图例,但在应用 plotly 后,图例消失了。 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: map.world1 数据的输出是这样的:

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我附上了一张 plot 之前的照片有传说的世界

guides(fill = guide_legend(title="")) seems to be the problematic argument. guides(fill = guide_legend(title=""))似乎是有问题的论点。 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).删除它会将图例放回ggplotly object 中。但是,图例标题仍然存在(我假设你想删除它,因为有问题的参数删除了图例标题)。 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.这似乎是与ggplotly() 和 legends相关的持续问题之一,因此我不确定您的问题是否有明确的答案。

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() .编辑: 根据这篇文章,您可能必须在ggplot()之外使用函数构建自己的图例,然后使用layout()将该图例添加到plotly() ) 中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM