Skip to content

Subplot does not work correctly when shareY = TRUE #1774

@nitram9

Description

@nitram9

I wanted to create a subplot where the Y axis is shared when plotting data on the X axis. The Y axis contains strings as shown in test_df[,1].

However, since I cannot seem to force the order of the Y variable, the code (below) produces this plot:

Screen Shot 2020-05-20 at 14 17 19

# load library if not already done
library(plotly)

# create demo dataframe
test_df <- data.frame(c("one","two","three","four","five","six","seven","eight"), stringsAsFactors = FALSE)
colnames(test_df) <- "category"
test_df$list1 <- c( 20 , 40 , 60 , 80 , 10 , 20 , 30 , 100 )
test_df$list2 <- c( 25 , 5 , 5 , 10 , 50 , 10 , 20 , 0 )
test_df$list3 <- c( 25 , 5 , 5 , 10 , 50 , 10 , 20 , 0 )

# create subplots
p1 <- plot_ly(data= test_df, x = test_df[[2]], y= test_df[[1]], name='List 1', type='bar', marker = list(color = 'rgb(255, 128, 128)'))
p2 <- plot_ly(data= test_df,  x = test_df[[3]], y= test_df[[1]], name='List 2', type='bar', marker = list(color = 'rgb(230, 0, 0)'))
p3 <- plot_ly(data= test_df, x = test_df[[4]],  y= test_df[[1]], name='List 3', type='bar', marker = list(color = 'rgb(131, 134, 135)'))
p<-subplot(p1, p2, p3, nrows = 1, shareX = FALSE, shareY = TRUE) %>%
  layout(
      xaxis = list(title = "values 1", tickangle = 90),
      xaxis2 = list(title = "values 2", tickangle = 90),
      xaxis3 = list(title = "values 3", tickangle = 90),
      yaxis = list(title = "Categories", showgrid = TRUE),
      legend = list(orientation = 'h', y = 1.2)
    )

# generate plot
p

Since I want to order the Y axis as one, two, three, etc... and if I understand Plotly correctly, I can order the Y axis titles properly by using 1) a column from the data frame with numbers such that the order is correct, and then 2) using tickvals and ticktext to overwrite the Y axis labels.

When I try to do the first part of this, I get a plot that I think may be a bug. This is the resulting plot and the code :

Screen Shot 2020-05-20 at 14 26 36

# load library if not already done
library(plotly)

# create demo dataframe
test_df <- data.frame(c("one","two","three","four","five","six","seven","eight"), stringsAsFactors = FALSE)
colnames(test_df) <- "category"
test_df$list1 <- c( 20 , 40 , 60 , 80 , 10 , 20 , 30 , 100 )
test_df$list2 <- c( 25 , 5 , 5 , 10 , 50 , 10 , 20 , 0 )
test_df$list3 <- c( 25 , 5 , 5 , 10 , 50 , 10 , 20 , 0 )
# add extra list to the df here
test_df$number_list <- c(1, 2, 3, 4, 5, 6, 7, 8)

# create subplots and now use the 5th column
p1 <- plot_ly(data= test_df, x = test_df[[2]], y= test_df[[5]], name='List 1', type='bar', marker = list(color = 'rgb(255, 128, 128)'))
p2 <- plot_ly(data= test_df,  x = test_df[[3]], y= test_df[[5]], name='List 2', type='bar', marker = list(color = 'rgb(230, 0, 0)'))
p3 <- plot_ly(data= test_df, x = test_df[[4]],  y= test_df[[5]], name='List 3', type='bar', marker = list(color = 'rgb(131, 134, 135)'))
p<-subplot(p1, p2, p3, nrows = 1, shareX = FALSE, shareY = TRUE) %>%
  layout(
      xaxis = list(title = "values 1", tickangle = 90),
      xaxis2 = list(title = "values 2", tickangle = 90),
      xaxis3 = list(title = "values 3", tickangle = 90),
      yaxis = list(title = "Categories", showgrid = TRUE),
      legend = list(orientation = 'h', y = 1.2)
    )

# generate plot
p

I also tried using a single plot but the same behavior continues. Am I making an obvious mistake or is this indeed a bug?

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions