himanshu tripathi
himanshu tripathi

Reputation: 101

Two X-axis in Plotly for R

https://community.plot.ly/t/how-to-plot-multiple-x-axis-in-plotly-in-r/3014/3?u=him4u324

I have posted my question on Plotly community as well

I am trying to display two x-axis with common Y-axis on plotly for R. I was able to do so as well but starting point for each x-axis is separated from each other Whereas I wish them to be represented a common y-axis.

2 X-axis with common Y-axis

f1 <- list(
  family = "Arial, sans-serif",
  size = 18,
  color = "grey"
)
f2 <- list(
  family = "Old Standard TT, serif",
  size = 14,
  color = "#4477AA"
)

# bottom x-axis
ax <- list(
  title = "Number of PBIs",
  titlefont = f1,
  anchor = "y",
  side = "bottom",
  showticklabels = TRUE,
  tickangle = 0,
  tickfont = f2

)

# top x-axis
ax2 <- list(
  title = " ",
  overlaying = "x",
  anchor = "y",
  side = "top",
  titlefont = f1,
  showticklabels = TRUE,
  tickangle = 0,
  tickfont = f2

)

# common y-axis
ay <- list(
  title = "Process & Sub-Process Areas",
  titlefont = f1,
  showticklabels = TRUE,
  tickangle = 0,
  tickfont = f2

)

plot_ly(data = scrum %>%
          group_by(Master.Project) %>%
          summarise(Total_PBIs_Planned=sum(PBIs.Planned.in.Sprint, na.rm = TRUE),
                    Total_PBIs_Delivered = sum(Actual.PBI.Delivery,na.rm = TRUE)) %>%           inner_join(scrum %>%  count(Master.Project)), # creating the desired data frame
        color = I("#149EF7")) %>% 
  # for bottom x-axis
  add_segments(x = ~Total_PBIs_Planned, xend = ~Total_PBIs_Delivered, 
               y = ~Master.Project, yend = ~Master.Project, showlegend = FALSE) %>%
  add_trace(x = ~Total_PBIs_Planned, y = ~Master.Project, 
            name = "Total_PBIs_Planned", type = 'scatter',mode = "markers",
            marker = list(color = "#149EF7", size = 15, 
                          line = list(color = '#FFFFFF', width = 1))) %>%
  add_trace(x = ~Total_PBIs_Delivered, y = ~Master.Project, 
            name = "Total_PBIs_Delivered",type = 'scatter',mode = "markers",
            marker = list(symbol ="circle-dot",color = "#F71430", size = 10, 
                          line = list(color = '#FFFFFF', width = 1))) %>%
  # for top x-axis
  add_trace(x = ~n, y = ~Master.Project, xaxis = "x2",
            name = "No._of_Sub_projects",type = 'bar', 
            marker = list(color = "#149EF7"),
            opacity = 0.1,
            hoverinfo = 'text',
            text = ~paste(
              Master.Project,
              '<br> Total Sub Projects: ',n,
              '<br> PBIs Planned: ',Total_PBIs_Planned,
              '<br> PBIs Delivered: ',Total_PBIs_Delivered
              )
            ) %>% 
  plotly::layout(
    title = "Product Backlog Items - Planned Vs Delivered",   titlefont = f1,
    xaxis = ax, 
    yaxis = ay,
    xaxis2 = ax2,
    margin = list(l = 250)
  ) 

Upvotes: 2

Views: 2606

Answers (1)

cburd
cburd

Reputation: 46

You want to use rangemode = "tozero" in your axis layout.

ax <- list(
  title = "Number of PBIs",
  titlefont = f1,
  anchor = "y",
  side = "bottom",
  showticklabels = TRUE,
  tickangle = 0,
  tickfont = f2,
  rangemode = 'tozero')

Or you can specify your specific ranges using

range = c(0, 5)

See Plotly help here: https://plot.ly/r/axes/#rangemode

Upvotes: 2

Related Questions