Search code examples
rplotlypie-chartlegend-properties

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


I have a plotly pie chart showing the number of people that have travelled to different countries. The list of countries is quite long and the plotly legend does not show them all in the window. There is a scroll-bar that one can use to see later elements in the list, but it is not very obvious and also not useful if I want to take a snapshot (static image) of the plotly figure since one can't see all the countries at the same time.

Does anyone know how to expand the height of the window that the legend is visible in? I am saving the plotly figure to html and would like to be able to see the full legend at once in the html file.

Here is an example:

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

I was thinking that the margin = list(...) option might have an argument for expanding the height of the legend window but can't find it. Any ideas would be much appreciated.


Solution

  • A colleague has provided this answer:

    In this example it looks like the thing that restricts the length of the legend is the base of the pie chart. If you want the full legend to be listed you need to add the height parameter to your plot_ly function:

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