Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

legend.position always 'right' in ggplotly, except when legend.position = 'none' #1049

Open
d4tagirl opened this issue Jun 18, 2017 · 7 comments
Labels

Comments

@d4tagirl
Copy link

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.

@wendywangwwt
Copy link

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

@tbradley1013
Copy link

tbradley1013 commented Sep 15, 2017

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.

@CaptainKatsuura
Copy link

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?

@tbradley1013
Copy link

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.

@seabbs
Copy link

seabbs commented Jan 31, 2018

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

@conrad-mac
Copy link

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

@cpsievert
Copy link
Collaborator

cpsievert commented Oct 8, 2018

In plotly.js, layout.legend is different from marker.colorbar. Displaying a colorbar horizontally currently isn't possible plotly/plotly.js#1244

@plotly plotly locked and limited conversation to collaborators Oct 8, 2018
cpsievert added a commit that referenced this issue Feb 6, 2020
cpsievert added a commit that referenced this issue Feb 6, 2020
cpsievert added a commit that referenced this issue Feb 6, 2020
cpsievert added a commit that referenced this issue May 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants