当数量更多时,ggplotly无法正常工作 [英] ggplotly not working properly when number are facets are more

查看:86
本文介绍了当数量更多时,ggplotly无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含仪表盘的闪亮仪表盘. Plotly是一个很棒的程序包,它为用户提供了出色的可视化和交互性.但是,当我使用ggplotly将ggplot图转换为交互式图形时,我没有得到预期的输出,并且当输出中的构面数量更多时,问题会成倍增加.例如,请找到下面的代码.

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)
)

用于生成图形的代码

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"))

这是我不使用ggplotly时的输出

This is the Output when i don't use ggplotly

这是ggplotly.

This is with ggplotly.

我在使用ggplotly时面临的问题是

The problems which i face when i use ggplotly is

  1. 轴和图例值被切掉.
  2. 我们可以很好地看到第二行中的图形受到挤压并且看起来不太好的区别.

总有办法克服这个特殊问题吗?我也将在闪亮的仪表板中使用这些图形.请让我知道您的建议.

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.

预先感谢.

大卫

推荐答案

增加绘图(整体对象)和面板(每个图形)周围的边距以解决此问题.重新设置图例标题也将很有帮助.

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"))

这篇关于当数量更多时,ggplotly无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆