1
votes

I am plotting a bar graph using three series for below dataframe:

library(plotly)
library(dplyr)

groups = c("high","low")
A1 = runif(2, min = 0, max = 1)
A2 = runif(2, min = 0, max = 1)
B1 = runif(2, min = 0, max = 1)
B2 = runif(2, min = 0, max = 1)
C1 = runif(2, min = 0, max = 1)
C2 = runif(2, min = 0, max = 1)

dfPlot = data.frame(groups, A1, A2,B1, B2,C1, C2 )

A2, B2 and C2 have to be plotted as benchmark data. I have used below codes to plot A1,B1 and C1 in bars and then plotted the benchmark as overlay bars. I also tried to plot the benchmark values as points but were not looking good.

chrt_title <- list(
  family = "Arial",
  size = 14,
  color ="#555555")  
ax <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)


tit<-function(txt) {
  jt<-  list(
    text = txt,
    font = chrt_title,
    xref = "paper",
    yref = "paper",
    yanchor = "bottom",
    xanchor = "center",
    align = "center",
    x = 0.5,
    y = 0,
    showarrow = FALSE, yshift=-30
  ) 
  
  return(jt)
  
}

cl<-c("#33C9FF", "#3385FF")

k<-plot_ly(x = dfPlot [,1],
           y = dfPlot [,2],
           type = 'bar', name = "Total", marker=list(color=cl))
k<-k %>%
  add_trace(x = dfPlot[,1],
            y = dfPlot[,3],
            type = 'bar', width = 0.3, name = "Benchmark",marker=list(color="#FFAB33"))


k<-k %>%  layout(xaxis = ax,barmode = 'overlay',  showlegend=TRUE, annotations=tit("AA"), bargap=0.01)
j<-plot_ly(x = dfPlot [,1],
           y = dfPlot [,4],
           type = 'bar', name = "Total", marker=list(color=cl))
j<-j %>%
  add_trace(x = dfPlot[,1],
            y = dfPlot[,5],
            type = 'bar', width = 0.3, name = "Benchmark",marker=list(color="#FFAB33"))
j<-j %>%  layout(xaxis = ax,barmode = 'overlay',  showlegend=FALSE, annotations=tit("BB"), bargap=0.01)


m<-plot_ly(x = dfPlot [,1],
           y = dfPlot [,6],
           type = 'bar', name = "Total", marker=list(color=cl))
m<-m %>%
  add_trace(x = dfPlot[,1],
            y = dfPlot[,7],
            type = 'bar', width = 0.3, name = "Benchmark",marker=list(color="#FFAB33"))
m<-m %>%  layout(xaxis = ax,barmode = 'overlay',  showlegend=FALSE, annotations=tit("CC") , bargap=0.01)

  
all<-subplot(k,j,m, nrows=1,  shareY = TRUE,margin = 0.008 )
all<-all%>% layout( legend = list(orientation = "h",
                                  xanchor = "center",  
                                  x = 0.5, y = 7),  showlegend=TRUE)
all

The problem with overlay bar chart is that the legends are not ok. There are three colours in bars , so i need three legends, with this chart the legends are repeting. My problem will be resolved if either the legends with this code is corrected or the benchmark can be presented differently as points with legends.

Thanks is advance for all suggestions!

1

1 Answers

0
votes

You can use legendgroup and hide the duplicates:

library(plotly)
library(dplyr)

groups = c("high", "low")
A1 = runif(2, min = 0, max = 1)
A2 = runif(2, min = 0, max = 1)
B1 = runif(2, min = 0, max = 1)
B2 = runif(2, min = 0, max = 1)
C1 = runif(2, min = 0, max = 1)
C2 = runif(2, min = 0, max = 1)

dfPlot = data.frame(groups, A1, A2, B1, B2, C1, C2)

chrt_title <- list(family = "Arial",
                   size = 14,
                   color = "#555555")
ax <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)

tit <- function(txt) {
  jt <-  list(
    text = txt,
    font = chrt_title,
    xref = "paper",
    yref = "paper",
    yanchor = "bottom",
    xanchor = "center",
    align = "center",
    x = 0.5,
    y = 0,
    showarrow = FALSE,
    yshift = -30
  )
  
  return(jt)
}

cl <- c("#33C9FF", "#3385FF")

k <- plot_ly(
  x = dfPlot [, 1],
  y = dfPlot [, 2],
  type = 'bar',
  name = "Total",
  legendgroup = "Total",
  marker = list(color = cl)
)
k <- k %>%
  add_trace(
    x = dfPlot[, 1],
    y = dfPlot[, 3],
    type = 'bar',
    width = 0.3,
    name = "Benchmark",
    legendgroup = "Benchmark",
    marker = list(color = "#FFAB33")
  )

k <-
  k %>%  layout(
    xaxis = ax,
    barmode = 'overlay',
    showlegend = TRUE,
    annotations = tit("AA"),
    bargap = 0.01
  )
j <- plot_ly(
  x = dfPlot [, 1],
  y = dfPlot [, 4],
  type = 'bar',
  name = "Total",
  legendgroup = "Total",
  showlegend = FALSE,
  marker = list(color = cl)
)
j <- j %>%
  add_trace(
    x = dfPlot[, 1],
    y = dfPlot[, 5],
    type = 'bar',
    width = 0.3,
    name = "Benchmark",
    legendgroup = "Benchmark",
    showlegend = FALSE,
    marker = list(color = "#FFAB33")
  )
j <-
  j %>%  layout(
    xaxis = ax,
    barmode = 'overlay',
    showlegend = FALSE,
    annotations = tit("BB"),
    bargap = 0.01
  )


m <- plot_ly(
  x = dfPlot [, 1],
  y = dfPlot [, 6],
  type = 'bar',
  name = "Total",
  legendgroup = "Total",
  showlegend = FALSE,
  marker = list(color = cl)
)
m <- m %>%
  add_trace(
    x = dfPlot[, 1],
    y = dfPlot[, 7],
    type = 'bar',
    width = 0.3,
    name = "Benchmark",
    legendgroup = "Benchmark",
    showlegend = FALSE,
    marker = list(color = "#FFAB33")
  )
m <-
  m %>%  layout(
    xaxis = ax,
    barmode = 'overlay',
    showlegend = FALSE,
    annotations = tit("CC") ,
    bargap = 0.01
  )

all <- subplot(k,
               j,
               m,
               nrows = 1,
               shareY = TRUE,
               margin = 0.008)
all <- all %>% layout(
  legend = list(
    orientation = "h",
    xanchor = "center",
    x = 0.5,
    y = 7
  ),
  showlegend = TRUE
)
all

result