【问题标题】:Remove Gaps Between Bars in Plotly删除 Plotly 中的条之间的间隙
【发布时间】:2020-03-14 09:50:29
【问题描述】:

我正在尝试使用 Plotly 在 R 中创建 Marimekko 图表。本质上,这只是一个堆叠的、可变宽度的条形图,两个条形直接相邻。目前,我的尝试如下所示:

创建它的代码在这里:

bar.test <- plot_ly(type = "bar") %>% 
  layout(title = paste0("Newark Charter vs District BTO Makeup"),
         xaxis = list(title = ""),
         yaxis = list(title = "Percent BTO", tickformat = "%")) %>% 
  add_trace(x = ~test1$sch.type, y = ~test1$y, width = ~test1$width, 
            marker = list(color = c(blue.dark.opq, red.opq.2, blue.dark.opq, red.opq.2) ,
                            line = list(color = 'rgba(0,0,0,1)' , width = 2))) %>% 
  add_annotations(x = ~test1$sch.type, y = ~test1$annotation.y,
                  text = paste0("<b>", 100*round(test1$y, 3), "%"),
                  showarrow = F,
                  font = list(size = 14, color = 'rgba(0,0,0,1)')) %>% 
  add_annotations(x = ~test1$sch.type, y = ~test1$all.y,
                  text = paste0(test1$all.count), showarrow = F,
                  font = list(size = 14, color = 'rgba(0,0,0,1)')) %>% 
  hide_legend()

数据看起来像这样:

我的目标只是让钢筋之间没有间隙。我曾尝试使用 bargap 参数来做到这一点,但已经读到为条形分配宽度会使 Plotly 忽略 bargap 参数。我还阅读了一个潜在的解决方法是手动更改条形的offset 参数。但是,我有许多具有不同宽度和百分比的数字,因此任何解决方案都不能是手动的。

【问题讨论】:

  • 无论如何,当您提供数据的名称时,您不需要使用~。使用~test1$

标签: r bar-chart r-plotly


【解决方案1】:

Marimekko 图通常被称为“马赛克图”。您可以使用ggmosaicggplot 中制作一个,然后使用ggplotly 将其转换为plotly。您没有在问题中提供可复制的数据,因此这里是使用 mtcars 数据集的示例:

library(dplyr)
library(ggmosaic)
library(plotly)

p <- mtcars %>%
       lapply(as.factor) %>%
       as.data.frame() %>%
       count(cyl, gear)  %>%
       ggplot() +
         geom_mosaic(aes(weight = n, x = product(cyl), fill = gear)) +
         labs(x = 'Cylinders', y = 'Gears')
ggplotly(p)

【讨论】:

    【解决方案2】:

    您可以通过自定义x 并在布局中使用bargap = 0 来做到这一点

    library(plotly)
    #> Loading required package: ggplot2
    #> 
    #> Attaching package: 'plotly'
    #> The following object is masked from 'package:ggplot2':
    #> 
    #>     last_plot
    #> The following object is masked from 'package:stats':
    #> 
    #>     filter
    #> The following object is masked from 'package:graphics':
    #> 
    #>     layout
    
    test1 <- data.frame(
        city.state = "Newark",
        sch.type = c("Charter", "Charter", "District", "District"),
        bto.stat = c(0,1,0,1),
        y = c(.7, .3, .1, .9),
        width = c(.3, .3, .7, .7),
        x = c(.15, .15, .65, .65),
        annotation.y = c(.3, .8, .05, .55),
        all.count = c(46000, 46000, 99000, 99000),
        all.y = c(1,1,1,1)
        )
    
    bar.test <- plot_ly(type = "bar") %>%
        add_trace(x = test1$x,
                  y = test1$y,
                  width = test1$width,
                  marker = list(
                    color = c("blue", "red", "blue", "red") ,
                    line = list(color = 'rgba(0,0,0,1)' , width = 2)
                    )
                  ) %>%
        layout(bargap = 0)
    
    bar.test
    

    reprex package (v0.3.0) 于 2019 年 11 月 19 日创建

    【讨论】:

    • 感谢您的建议。由于自定义 X 值,此解决方案有效。设置 bargap = 0 实际上并没有做任何事情。分配条形宽度时,Plotly 会忽略 bargap。我认为这很好地解决了我的问题,因为 X 值可以系统地计算出来,所以不需要手动输入。然后通过注释添加正确的标签,我如何首先从显示中删除 0.15 和 0.65?
    • 您可以使用tickmode = "array" 自定义轴刻度。 plot.ly/r/tick-formatting
    • plotly.js 相关问题:github.com/plotly/plotly.js/issues/930
    猜你喜欢
    • 2016-12-30
    • 2015-10-10
    • 2019-10-31
    • 2013-12-25
    • 2018-05-23
    • 2020-06-12
    • 2013-04-10
    • 2020-05-01
    • 2016-04-02
    相关资源
    最近更新 更多