简体   繁体   中英

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!

在此处输入图片说明

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM