Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - ggplotly not working properly when number are facets are more

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
description") + 
  theme(strip.placement = "outside",
        plot.margin = margin(t = 10, r = 5, b = 5, l = 20, "points"),
        panel.spacing = unit(10, "points"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...