簡體   English   中英

ggplot 和 ggplotly 之間圖例 position 的區別?

[英]Difference in legend position between ggplot and ggplotly?

我發現 ggplot 和 ggplotly 中的同一張圖表之間有一個有趣而奇怪的區別

income_gap_chart <- ggplot(income_gap, aes(x = Country, y = Percent, fill = Income)) + 
geom_bar(position = "dodge", stat = "identity") +
scale_fill_brewer(palette = "Set1") +
coord_flip() +
theme(axis.title.y = element_blank()) +
scale_y_continuous(limits = c(0, 100)) +
theme_tufte() +
theme(axis.title.y = element_blank()) +
theme(legend.position = "bottom")

對於 ggplot 它看起來很完美,底部有一個圖例標題

在此處輸入圖像描述

但是當我用 ggplotly() 包裝它時,圖例開始表現不同

在此處輸入圖像描述

我的問題 - 我想要第一個 ggplotly 格式的圖表,但無法解決這個問題,底部的圖例不起作用。 想法?

謝謝!

在一些 R 專家的幫助下很快解決了。

添加了這個

ggplotly(income_gap_chart) %>% layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

結果: 在此處輸入圖片說明

謝謝!

正如評論中指出的那樣,該解決方案似乎刪除了圖例標題。 這很容易通過在layout()中包含title =...來解決:

diamonds_chart <- ggplot(diamonds, aes(x = cut, y = carat, fill = color)) + 
  geom_bar(position = "dodge", stat = "identity")
  
diamonds_chart %>% 
  ggplotly %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.1, 
      title = list(text = 'My legend title')
      )
    )

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM