ggplot和ggplotly之间的图例位置差异?

Ole*_*siy 12 r legend ggplot2 ggplotly

我发现了 ggplot 和 ggplotly 中同一个图表之间有趣而奇怪的区别

income_gap_chart <- ggplot(income_gap, aes(x = Country, y = Percent, fill = Income)) + 
geom_bar(position = "dodge", stat = "identity") +
scale_fill_brewer(palette = "Set1") +
coord_flip() +
theme(axis.title.y = element_blank()) +
scale_y_continuous(limits = c(0, 100)) +
theme_tufte() +
theme(axis.title.y = element_blank()) +
theme(legend.position = "bottom")
Run Code Online (Sandbox Code Playgroud)

对于 ggplot 它看起来很完美,底部有一个图例标题

在此处输入图片说明

但是当我用 ggplotly() 包装它时,图例开始表现不同

在此处输入图片说明

我的问题 - 我想要 ggplotly 格式的第一个图表,但无法解决这个问题,底部的图例不起作用。想法?

谢谢!

Ole*_*siy 13

在一些 R 专家的帮助下很快解决了。

添加了这个

ggplotly(income_gap_chart) %>% layout(legend = list(orientation = "h", x = 0.4, y = -0.2))
Run Code Online (Sandbox Code Playgroud)

结果:在此处输入图片说明

谢谢!

  • 此解决方案中没有图例标题 (4认同)
  • 我收到错误:`布局错误(., legend = list(orientation = "h", x = 0.4, y = -0.2)) : 未使用的参数 (legend = list(orientation = "h", x = 0.4, y = -0.2))` (2认同)