2

I'm trying to produce in R a similar plotly plot as the one described in thiswebsite and produced using Python. It's a horizontal barplot which was created by stacking multiple subplot onto of each other.

The plot looks like:

enter image description here

I'm getting close but I can't fully manage to reproduce it. The way the plot is program is roughly by:

  • making one barplot for each category wanted, with the proper title and text
  • Stack all the plots with themake_subplot function
  • remove unnecessary label and format properly the text.

The biggest problem I'm facing so far is the fact I'm not able to repeat the title of each individual subplots within the main plot.

Here is my code. Notice that there is a different title for each plot:

library(plotly)# preparing bogus datad <- data.frame(question = paste0("Question ", 1:10),                value = seq(20, 1, -2))# setting up a list to store the plotsls_p <- NULL# Create one plot (subplot) for each categories in my datafor(i in 1:nrow(d)){  ls_p[[i]] <- plot_ly(d[i,], x=~value, y=~question) |>    add_bars(orientation='h') |> #    add_text(x=~value, y=~question, text=~value) |>    layout(title = list(text=d$question[i],                        xanchor = "left",                        align = "left"),           showlegend=F,           grid=list(showgrid=F),           xaxis=list(range=c(0,max(d$value)),                      showticklabels =F),           yaxis=list(showticklabels = F)           )}# regroup all the subplots togethersubplot(ls_p, nrows= nrow(d), shareX = T, which_layout = 1, titleX = F, titleY = F)

This code produces:

enter image description here

It is wrong a multiple ways:

  • Only the title of the first plot is written, all subsequent plots lack there titles
  • The title is not left align
  • text (values) is centered to the end of the bars, not kept in the inside right section
  • The first and last bar are thicker than the others.

It's there a way to produce this kind of graph in ploty-R or are we limited to plotly-python?

askedJan 19, 2023 at 14:24
Bastien's user avatar

1 Answer1

1

You could useannotations inlayout withtext instead of a title. Here is a reproducible example:

library(plotly)# preparing bogus datad <- data.frame(question = paste0("Question ", 1:10),                value = seq(20, 1, -2))# setting up a list to store the plotsls_p <- NULL# Create one plot (subplot) for each categories in my datafor(i in 1:nrow(d)){  ls_p[[i]] <- plot_ly(d[i,], x=~value, y=~question) |>    add_bars(orientation='h') |> #    add_text(x=~value, y=~question, text=~value) |>    layout(annotations = list(text = d$question[i],                              showarrow = FALSE),           showlegend=F,           grid=list(showgrid=F),           xaxis=list(range=c(0,max(d$value)),                      showticklabels =F),           yaxis=list(showticklabels = F)    )}# regroup all the subplots togethersubplot(ls_p, nrows= nrow(d), shareX = T, which_layout = 1, titleX = F, titleY = F)

Created on 2023-01-19 withreprex v2.0.2


Edit:

You could add anx coordinates to have the title on the left side like this:

library(plotly)# preparing bogus datad <- data.frame(question = paste0("Question ", 1:10),                value = seq(20, 1, -2))# setting up a list to store the plotsls_p <- NULL# Create one plot (subplot) for each categories in my datafor(i in 1:nrow(d)){  ls_p[[i]] <- plot_ly(d[i,], x=~value, y=~question) |>    add_bars(orientation='h') |> #    add_text(x=~value, y=~question, text=~value) |>    layout(annotations = list(text = d$question[i],                              showarrow = FALSE,                              x = 1.5),           showlegend=F,           grid=list(showgrid=F),           xaxis=list(range=c(0,max(d$value)),                      showticklabels =F),           yaxis=list(showticklabels = F)    )}# regroup all the subplots togethersubplot(ls_p, nrows= nrow(d), shareX = T, which_layout = 1, titleX = F, titleY = F)

Created on 2023-01-20 withreprex v2.0.2

answeredJan 19, 2023 at 18:11
Quinten's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, any idea how to left align the annotation? I've tried addingxanchor="right" andalign="left" to the annotation list and thealign doesn't seem to work.
Hi @Bastien, you could specify coordinates like x and y in the annotations function.
Hi @Bastien, Please check my edited answer! You can specify an x coordinate.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.