0

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

enter image description here

This is with ggplotly.

enter image description here

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

David Chris
  • 255
  • 4
  • 16

1 Answers1

0

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"))
  • Thanks for the Response :). I tried using it but the issue still exists for me. the Axis are getting cut and the facets in the middle row are squeezed. – David Chris Oct 08 '19 at 04:20
  • Do you want the columns to resize cleanly when the window is resized? One way to solve this neatly is to set an arbitrary column width, such as 200px. – Cam McMains Oct 10 '19 at 05:21