【问题标题】:ggplotly not working properly when number are facets are more当数量更多时,ggplotly无法正常工作
【发布时间】:2020-02-04 09:59:50
【问题描述】:

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

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 时的输出

这是 ggplotly。

我在使用 ggplotly 时遇到的问题是

  1. 轴和图例值被删除。
  2. 我们可以很清楚地看到第二行的图表被挤压并且看起来不太好的区别。

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

提前致谢。

大卫

【问题讨论】:

    标签: r ggplot2 shiny plotly ggplotly


    【解决方案1】:

    增加绘图(整个对象)和面板(每个图表)周围的边距以解决此问题。重新格式化您的图例标题也将有所帮助。

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

    【讨论】:

    • 感谢您的回复 :)。我尝试使用它,但问题仍然存在。轴被切割,中间行的刻面被挤压。
    • 您是否希望在调整窗口大小时完全调整列的大小?巧妙解决此问题的一种方法是设置任意列宽,例如 200 像素。
    猜你喜欢
    • 1970-01-01
    • 2016-06-28
    • 2014-06-11
    • 1970-01-01
    • 2018-06-19
    • 2016-12-10
    • 2013-11-12
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多