【问题标题】:Two X-axis in Plotly for RR中的两个X轴
【发布时间】:2018-07-29 14:53:35
【问题描述】:

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 轴。

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

【问题讨论】:

    标签: r r-plotly


    【解决方案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

    【讨论】:

    • 感谢您抽出时间来回答它,但它仍然不起作用。暂时我已经删除了零线以看起来整洁。
    • @himanshutripathi 也将rangemode = "tozero" 放入斧头列表中。我在上面输入了错误的变量。
    • 没问题@himanshutripathi,你能标记为答案吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多