plotly / plotly/plotly.R

Stackgroup with scatter and more than 1 axis not working properly?

Open
#2,099 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I am doing a scatter plot with stackgroups. I would like to use the x2 and y2 axises. When I use the regular x and y axis, the result is correct. The data frame below is used. The first Plotly chunk is for the regular x and y axis behavior. The second one tries to use x2 and y2 but only the 2nd stack group is shown. I think this might be a bug.

library(datasets)

df <- mtcars[1:5,]
df$mpg <- 0
df$qsec <- df$qsec + runif(length(df$qsec),min=-3, max=5)
df$hp <- df$hp + runif(length(df$hp),min=-30, max=30)
df$names <- rownames(df)
rownames(df) <- NULL

df2 <-  mtcars[1:5,]
df2$names <- rownames(df2)
rownames(df2) <- NULL

df<- rbind(df,df2)

Below works but since the y axis range is quite different for qsec and hp, a second y axis is needed.

library(dplyr)
library(plotly)

plot_ly(type = "scatter",mode='none', width = 800, height=420) %>%
          add_trace(
                  mode = "lines",
                  stackgroup = "red",
                  orientation = "h",
                  x = df$mpg,
                  y = df$qsec,
                  line = list(color = "red"),
                  name = df$names) %>%
add_trace(
                  mode = "lines",
                  stackgroup = "blue",
                  orientation = "h",
                  x = df$mpg,
                  y = df$hp,
                  line = list(color = "blue"),
                  name = df$names)%>%
          layout(  xaxis =list(title="mpg", side = "bottom"),
                   yaxis =list(title="qsec",side = "left"))

Here, the following is tried.

library(dplyr)
library(plotly)

plot_ly(type = "scatter",mode='none', width = 800, height=420) %>%
          add_trace(
                  mode = "lines",
                  stackgroup = "red",
                  orientation = "h",
                  x = df$mpg,
                  y = df$qsec,
                  line = list(color = "red"),
                  name = df$names) %>%
add_trace(
                  mode = "lines",
                  stackgroup = "blue",
                  orientation = "h",
                  xaxis = "x2",
                  yaxis = "y2",
                  x = df$mpg,
                  y = df$hp,
                  line = list(color = "blue"),
                  name = df$names)%>%
          layout(  xaxis =list(title="mpg", side = "bottom"),
                   yaxis =list(title="qsec",side = "left")
                  ,yaxis2 = list(title = "hp", side = "right", overlaying = "y")
                  ,xaxis2 = list(title = "mpg", side = "top", overlaying = "y")
                  )  

I also tried using fill = "tozerox" and fill = "tozeroy" instead of orientation="h"|"v" but this doesn't work at all. Thank you in advance for taking a look. :-)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two supplied R examples and compare the output when the second trace uses xaxis = "x2" and yaxis = "y2" with stackgroup. Trace how add_trace and layout handle stacked scatter traces across multiple axes; the issue is done when both stack groups render correctly with the secondary axes, or the supported limitation is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.