r - R 的 Plotly 中的两个 X 轴

标签 r r-plotly

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

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

我试图在 R 的绘图上显示两个具有公共(public) Y 轴的 x 轴。我也能够这样做,但每个 x 轴的起点彼此分开,而我希望将它们表示出来公共(public) y 轴。

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

最佳答案

您想在轴布局中使用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

关于r - R 的 Plotly 中的两个 X 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48865021/

相关文章:

r - R中更好的字符串插值

r - 实三次多项式的最快数值解?

r - 将XY点添加到由levelplot生成的栅格 map 中

r - 如何从 Shiny 的 ggplotly 中获取数据行

r - 粗体绘图 (R) 饼图标签

r - 使用 R 循环下载文件并重命名

r - 切掉R中的外部NA

将悬停标签重新放置在栏的中间,这样它就不会覆盖标题

r - Plotly,在用 add_markers 创建的点周围添加边框

r - 在更改要在 R Shiny 中绘制的变量时,防止绘图选择的迹线重置