R ggplotly:图例显示不正确

R ggplotly: legend is not correctly displayed

我对 plotly 包有疑问。图例无法正确显示或未显示所有值,因为它被剪切了!

我想在绘图区域之外(在底部或右角)获取图例。

我已经尝试更改 ggplot 中的位置:

legend.position="bottom"

完全没有结果...

然后我尝试了来自 plotly 网站的代码:

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

没用,图例在底部,但是被切掉了,在x轴标题后面...

这是来自 mtcars 数据集的示例代码:

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

ggplotly(a)

正如我们所看到的那样,图例的标题也被剪掉了..

如有任何帮助,我将不胜感激!

谢谢

您可以调整边距和绘图大小。你可以试试:

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)