问题
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
- The axis and the legend values are getting cut out.
- 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