ggplotly not working properly when number are facets are more

南楼画角 提交于 2020-01-24 01:05:05

问题


I am trying to create a Shiny Dashboard which contains Plots also. Plotly is such a great a package that it provides great visualization and interactivity to the users. But when i use ggplotly to convert my ggplot plots to interactive graphs, i am not getting the expected output and the issues get multiplied when the number of facets in the output are more. for example kindly find the code below.

Source_Data_dupli <-
data.frame(
key = c(1, 1, 1, 2, 2, 2, 3, 3, 3,4,4,5,5,6,7),
Product_Name = c(
  "Table",
  "Table",
  "Chair",
  "Table",
  "Bed",
  "Bed",
  "Sofa",
  "Chair",
  "Sofa",
  "Table",
  "Bed",
  "Chair",
  "Table",
  "Bed",
  "Bed"
),
Product_desc = c("XX", "XXXX", "YY", "X", "Z", "ZZZ", "A", "Y", 
"A","Y","XX", "XXXX", "YY", "X", "Z"),
Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6, 7,6,8,9,12,34)
)

The code used to generate the graph

ggplotly(Source_Data_dupli %>% 
ggplot(aes(Product_Name, Cost)) + 
geom_col(aes(fill = Product_desc), position = position_dodge(preserve = 
"single")) +
facet_wrap(~key, scales = "free_x", strip.position = "bottom") +
theme(strip.placement = "outside"))

This is the Output when i don't use ggplotly

This is with ggplotly.

The problems which i face when i use ggplotly is

  1. The axis and the legend values are getting cut out.
  2. We can very well see the difference that the graphs in the second row are squeezed and they don't look good.

Is there anyway to overcome this particular issue ? also i am going to use these graphs in the shiny dashboard. Kindly let me know your suggestions.

Thanks in Advance.

David


回答1:


Increase the margin around your plot (the overall object) and the panel (each of your graphs) to fix this. It will also be helpful to reformat your legend title.

  scale_fill_discrete(name = "Product\ndescription") + 
  theme(strip.placement = "outside",
        plot.margin = margin(t = 10, r = 5, b = 5, l = 20, "points"),
        panel.spacing = unit(10, "points"))


来源:https://stackoverflow.com/questions/58263918/ggplotly-not-working-properly-when-number-are-facets-are-more

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