【问题标题】:Mirror bar plot with plotly用 plotly 镜像条形图
【发布时间】:2019-09-24 18:47:06
【问题描述】:

我想知道如何垂直镜像两种条形(正数和负数),如下图所示:

到目前为止,我只能让它们像镜子一样并排站立:

数据:

library(plotly)
time <- c("2018-10","2018-11","2018-12")
add <- c(20,15,20)
delete <- c(-5,-10,-2)
total <- c(60,65,83)
df <- data.frame(time,add,delete,total)

plot_ly(df) %>%
    add_trace(x = ~time, y = ~add, type = 'bar', name = 'add',
              marker = list(color = '#33adff'),
              hovertemplate = paste('add: %{y}',
                                    '<br>time: %{x}<br>')) %>%
    add_trace(x = ~time, y = ~delete, type = 'bar', name = 'delete',
              marker = list(color = '#ff6666'),
              hovertemplate = paste('delete: %{y}',
                                    '<br>time: %{x}<br>'))

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    问题可以通过barmode="overlay"来解决。
    您需要将df数据集设置为长数据格式。

    library(plotly)
    time <- c("2018-10","2018-11","2018-12")
    add <- c(20,15,20)
    delete <- c(-5,-10,-2)
    total <- c(60,65,83)
    n <- length(time)
    df <- data.frame(time=rep(time,2), y=c(add, delete), grp=rep(c("Add","Delete"),each=n))
    
    plot_ly(df) %>%
        add_trace(x = ~time, y = ~y, color=~grp, text=~grp, type = 'bar', 
                  marker = list(color = c(rep('#33adff',n), rep('#ff6666',n))),
                  hovertemplate = paste('%{text}: %{y}', '<br>time: %{x}<br>')) %>%
        layout(barmode="overlay")
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2018-01-06
      • 2020-08-25
      • 2016-02-25
      • 2023-02-26
      • 2020-09-05
      相关资源
      最近更新 更多