R 带plotly的镜像条形图

R 带plotly的镜像条形图,r,plotly,R,Plotly,我想知道如何在垂直方向上镜像两种条形图(正数和负数),如下图所示: 到目前为止,我只能让他们像镜子一样并排站着,就像: 数据: library(plotly) 时间可以使用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(6

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

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

数据:

library(plotly)

时间可以使用
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")
library(plotly)
时间
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")