1

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

我也在 Plotly 社区上发布了我的问题

我正在尝试在 R 图中显示两个具有共同 Y 轴的 x 轴。我也能够这样做,但是每个 x 轴的起点彼此分开而我希望它们被表示为一个共同的 y -轴。

2 X轴,共用Y轴

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)
  ) 
4

1 回答 1

1

您想rangemode = "tozero"在轴布局中使用。

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

或者您可以使用指定您的特定范围

range = c(0, 5)

在此处查看 Plotly 帮助:https ://plot.ly/r/axes/#rangemode

于 2018-07-30T19:53:47.650 回答