簡體   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