R plotly pie chart - 如何显示所有长图例?

R plotly pie chart - how to display all of a long legend?

我有一个饼图,显示去过不同国家的人数。国家列表很长,情节图例并未在 window 中显示所有国家/地区。有一个滚动条,可以用来查看列表中后面的元素,但它不是很明显,如果我想拍摄图形的快照(静态图像)也没有用,因为一个人看不到全部同时国家。

有谁知道如何扩展图例可见的 window 的高度?我正在将绘图保存到 html,并希望能够在 html 文件中立即看到完整的图例。

这是一个例子:

# Load relevant libraries:

library(data.table)
library(plotly)
library(ggplot2)
library(RColorBrewer)
library(htmlwidgets)

# Example data set:

mydt <- structure(list(country = c("afghanistan", "african continent", 
"albania", "algeria", "american samoa", "asian continent", "australia", 
"bahamas", "bangladesh", "barbados", "bolivia", "bosnia and herzegovina", 
"botswana", "brazil", "bulgaria", "cambodia", "cameroon", "canada", 
"cape verde", "caribbean", "china", "colombia", "congo", "costa rica", 
"croatia", "cuba", "cyprus", "domestic", "dominica", "dominican republic", 
"egypt", "eritrea", "ethiopia", "european continent", "falkland island malvinas", 
"france", "gambia", "ghana", "greece", "hong kong", "hungary", 
"india", "indonesia", "iraq", "ireland", "israel", "italy", "jamaica", 
"jersey", "jordan", "kenya", "kuwait", "libyan arab jamahiriya", 
"lithuania", "macau", "macedonia the former yugoslav republic of", 
"malaysia", "maldives", "malta", "mauritius", "mexico", "middle east region", 
"morocco", "nepal", "nigeria", "no travel history", "pakistan", 
"panama", "peru", "philippines", "poland", "portugal", "qatar", 
"russian federation", "saint kitts and nevis", "saudi arabia", 
"scotland", "serbia", "sierra leone", "singapore", "south africa", 
"south east asia", "spain", "sri lanka", "sudan", "switzerland", 
"taiwan province of china", "tanzania united republic of", "thailand", 
"tunisia", "turkey", "united arab emirates", "united states", 
"unknown destination", "uzbekistan", "viet nam", "zimbabwe"), 
    people = c(2L, 6L, 2L, 1L, 1L, 13L, 1L, 1L, 9L, 2L, 1L, 2L, 
    1L, 2L, 2L, 1L, 1L, 1L, 12L, 2L, 5L, 2L, 2L, 3L, 1L, 12L, 
    9L, 392L, 1L, 6L, 25L, 1L, 1L, 3L, 1L, 3L, 2L, 5L, 5L, 1L, 
    1L, 78L, 17L, 4L, 13L, 1L, 1L, 2L, 1L, 1L, 4L, 1L, 1L, 2L, 
    1L, 1L, 5L, 2L, 1L, 1L, 18L, 1L, 21L, 3L, 3L, 1166L, 32L, 
    6L, 6L, 4L, 6L, 8L, 1L, 4L, 1L, 4L, 1L, 1L, 2L, 3L, 1L, 1L, 
    26L, 3L, 1L, 1L, 1L, 3L, 45L, 9L, 33L, 7L, 3L, 41L, 1L, 10L, 
    1L)), .Names = c("country", "people"), sorted = "country", class = c("data.table", 
"data.frame"), row.names = c(NA, -97L))

# Create pie chart:
travelpie = plot_ly(mydt, labels = mydt$country, values = mydt$people, type = "pie") %>%
    layout(title = "Travel destinations", 
           showlegend = T, margin = list(l = 50, r = 50, t = 50, b = 450))

# Write to html file:
saveWidget(travelpie, file = "My travel pie.html")

我在想 margin = list(...) 选项可能有扩展图例高度的参数 window 但找不到。任何想法将不胜感激。

一位同事提供了这个答案:

在这个例子中,看起来限制图例长度的是饼图的底部。如果你想列出完整的图例,你需要将高度参数添加到你的 plot_ly 函数中:

travelpie = plot_ly(mydt, labels = mydt$country, values = mydt$people, type = "pie", 
                    height = 2500) %>%
            layout(title = "Travel destinations", 
                   showlegend = T, margin = list(l = 50, r = 50, t = 50, b = 450))