Difference in legend position between ggplot and ggplotly?

余生长醉 提交于 2019-12-24 11:14:20

问题


I find out an interesting and strange difference between the same chart in ggplot and 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")

For ggplot it looks perfect with a legend title at the bottom

But when I wrap this with ggpotly(), the legend starts to behave differently

My problem - I want the first chart in ggplotly format, but cannot fix this issue and legend at the bottom does not work. Ideas?

Thanks!


回答1:


Resolved pretty fast with the help of some R experts.

Added this

ggplotly(income_gap_chart) %>% layout(legend = list(orientation = "h", x = 0.4, y = -0.2))

Result:

Thanks!



来源:https://stackoverflow.com/questions/54451001/difference-in-legend-position-between-ggplot-and-ggplotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!