Plotly: legend.position always 'right' in ggplotly, except when legend.position = 'none'

Created on 18 Jun 2017  路  7Comments  路  Source: ropensci/plotly

When plotting a ggplot object using ggplotly, legend.position is always 'right' even if I chose legend.position = 'top' or any of the other options. It only works as it is supposed to when I specify legend.position = 'none' that it actually disappears.

ggplot displays the legend fine.

library(tibble)
library(ggplot2)

df <- tibble(year = c(2000, 2001, 2000, 2001),
             freq = c(3, 4, 2, 9),
             clas = c("A", "A", "B", "B"))

ggplot(df, aes(year, freq, color = clas)) +
  geom_line() +
  theme(legend.position = 'top')

When I try this instead:

library(plotly)

ggplotly(
ggplot(df, aes(year, freq, color = clas)) +
  geom_line() +
  theme(legend.position = 'top')
)

I get the legend on the right, as I show in this shiny app.

ggplotly

Most helpful comment

Hi, I am having issues getting the suggested solution to work. Adding the layout seems to be ignored. I may be missing something obvious here, but I have been fiddling with it for some time. I've tried various x and y values as well as specifying only the orientation and vice versa.

Example:

library(ggplot2)
library(magrittr)
library(plotly)


plot <- ggplot(mtcars, aes(x = mpg, y = disp, col = qsec)) + 
geom_point() + 
theme(legend.position = "bottom") 

ggplotly(plot) %>%
layout(legend = list(orientation = "h", x = -0.5, y =-1))

Which produces the following:

  • ggplot

screenshot 2018-01-31 14 39 36

  • ggplotly
    screenshot 2018-01-31 14 34 04

Session information:

screenshot 2018-01-31 14 37 55

All 7 comments

Same problem.. I tried to set legend argument in ggplotly() but it doesn't work either

you would need to set the legend orientation option in the layout function in the plotly package. It would look like this:

ggplotly(
  ggplot(df, aes(year, freq, color = clas)) +
    geom_line() +
    theme(legend.position = 'top')
) %>%
  layout(legend = list(
      orientation = "h"
    )
  )

However, there are some limitations with this option as it does not automatically wrap the legend as ggplot does. This will cause legend with a large number of items or long names to be cut off. You can see this issue for more information. It seems that they implemented some kind of fix for this problem in the plotly.js library, but I have not figured out how to implement it in R from their documentation. However, if you do not have a long legend, then the orientation = "h" option should work for you.

As a side note, you will likely have to play around with the positioning of the legend in the layout. You will need to set the x, xanchor, y, and yanchor options. It would be set by adding the arguments and there value to the same list as the orientation option. It would look like this:

layout(
legend = list(
    orientation = "h",
    x = -0.5,
    ...
  )
)

Check out their legend options to see what these arguments can be set to.

Hi, I did this for a similar issue but
legend = list( orientation = "h", x = -0.5, ... )

puts the legend at the top of the plot. I tried fixing this by doing something like y=-4 but the problem with that is it messes with the y axis title and axis labels. Any ideas?

If you look at the legend layout options, it says that the x and y arguments have to be a number between -2 and 3.

Hi, I am having issues getting the suggested solution to work. Adding the layout seems to be ignored. I may be missing something obvious here, but I have been fiddling with it for some time. I've tried various x and y values as well as specifying only the orientation and vice versa.

Example:

library(ggplot2)
library(magrittr)
library(plotly)


plot <- ggplot(mtcars, aes(x = mpg, y = disp, col = qsec)) + 
geom_point() + 
theme(legend.position = "bottom") 

ggplotly(plot) %>%
layout(legend = list(orientation = "h", x = -0.5, y =-1))

Which produces the following:

  • ggplot

screenshot 2018-01-31 14 39 36

  • ggplotly
    screenshot 2018-01-31 14 34 04

Session information:

screenshot 2018-01-31 14 37 55

In response to @seabbs post, it seems like the problem occurs only for continuous variables. When using the transmission variable for the colour aesthetic (and converting it to factor), the layout() option seems to work (with some minor tweaks for x and y values).

library(ggplot2)
library(magrittr)
library(plotly)

plot <- ggplot(mtcars, aes(x = mpg, y = disp, col = factor(am))) + 
  geom_point() + 
  theme_bw() +
  theme(legend.position = "bottom", legend.title = element_blank()) 

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

ggplot() -

ggplot

ggplotly() -

ggplotly

In plotly.js, layout.legend is different from marker.colorbar. Displaying a colorbar horizontally currently isn't possible https://github.com/plotly/plotly.js/issues/1244

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fabiangehring picture fabiangehring  路  4Comments

escorial82 picture escorial82  路  6Comments

cpsievert picture cpsievert  路  5Comments

cpsievert picture cpsievert  路  5Comments

cldougl picture cldougl  路  6Comments