繁体   English   中英

使用 ggplotly() 时 ggplot 图例消失

[英]ggplot legend disappears when using ggplotly()

我试图保留使用 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")) 

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

我附上了一张 plot 之前的照片有传说的世界

guides(fill = guide_legend(title=""))似乎是有问题的论点。 删除它会将图例放回ggplotly object 中。但是,图例标题仍然存在(我假设你想删除它,因为有问题的参数删除了图例标题)。 此外,图例从因子转换为连续。 这似乎是与ggplotly() 和 legends相关的持续问题之一,因此我不确定您的问题是否有明确的答案。

编辑: 根据这篇文章,您可能必须在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