Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplotly: legend is not correctly displayed

I do have a problem with plotly package. The legend does not display correctly or does not show all the values cause it is cut!

enter image description here

I would like to get the legend outside of the plot area (at the bottom or right corner).

I have already tried to change the position in ggplot:

legend.position="bottom"

no result at all...

then i have tried code from plotly website:

p %>% layout(legend = list(x = 0.5, y = -100))

it did not work, the legend was at the bottom, but it was cut, and behind x axis title...

Here is a sample code from mtcars dataset:

a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
  geom_boxplot()

ggplotly(a)

as we can see the title of the legend is cut there as well..

I would appreciate any help!

Thanks

like image 789
Mal_a Avatar asked Apr 04 '16 11:04

Mal_a


1 Answers

You could play with the margin and plot size. You can try:

m = list(
  l = 100,
  r = 40,
  b = 100,
  t = 50,
  pad = 0
)
a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
  geom_boxplot()
a %>% layout(autosize = F, width = 800, height = 600, margin = m)
like image 63
MLavoie Avatar answered Sep 28 '22 06:09

MLavoie