【问题标题】:R plotly x-axis use labels for displayR plotly x轴使用标签进行显示
【发布时间】:2021-04-18 03:46:54
【问题描述】:

尝试使用 R Plotly 绘制条形图。我需要按 q_order 排序的图表 x 轴,但需要让 x 轴显示来自名为 title 的数据的标签。我该怎么做?

数据

图表

代码

 fig <- plot_ly(
  data = ybq,
  x = ~q_order,
  y = ~t_put,
  type = "bar"
) %>%
  layout(xaxis=list(
               autorange="reversed", 
               type="category"
               ))

【问题讨论】:

    标签: r plotly axis-labels x-axis


    【解决方案1】:

    您可以根据q_order 中的值排列title 的因子水平。

    library(plotly)
    
    ybq$title <- factor(ybq$title, ybq$title[order(ybq$q_order)])
    
    plot_ly(
      data = ybq,
      x = ~title,
      y = ~q_order,
      type = "bar"
    )
    

    数据

    ybq <- data.frame(title = c('21-Jan', 'Q4 2020', 'Q3 2020', 'Q2 2020'), 
                      t_put = c(1606, 11419, 10882, 6525), 
                      q_order = c(112021, 42020,32020, 22020))
    

    【讨论】:

    • 我试图使用重新排序而不是您提到的方法。谢谢!
    猜你喜欢
    • 2020-10-06
    • 1970-01-01
    • 2022-11-29
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多