【问题标题】:plotly - show specific dates in x-axis labels/ticksplotly - 在 x 轴标签/刻度中显示特定日期
【发布时间】:2020-04-14 20:29:36
【问题描述】:

我一直在尝试创建一个线图,其中 x 轴由日期组成,y 是一个连续变量。现在,x 轴标签只显示年份,但我想要专门针对 2017 年 7 月、2018 年 7 月、2019 年 7 月、...、2022 年 7 月的标签...我尝试使用以下代码指定刻度,但输出没有给我我想要的。有什么方法可以实现我想要的输出?

p <- plot_ly(mode = 'lines', hoverinfo = "x+y")
p <- p %>%
  add_trace(name = "Cumulative enrollment", type = "scatter", x = test2$month, y = test2$cumsum, color = I("blue")) %>%
  add_lines(x = pred$month, y = pred$cumsum, color = I("red"), linetype = I("dash"), name = 'Projected enrollment \n (19 subjects/month)') %>%
  layout(xaxis = list(autotick = F, tickmode = "array", tickvals = c(as.Date('2017-07-01'), as.Date('2018-07-01'), as.Date('2019-07-01'), as.Date('2020-07-01'), as.Date('2021-07-01'), as.Date('2022-07-01'))),
         yaxis = list(title = "Number of Subjects (Cumulative)", tickmode = "array", tickvals = c(0, 200, 400, 600, 800, 1000, 1109, 1200)), 
         shapes = list(hline(1109)), 
         legend = list(orientation = 'h', xanchor = "center", x = 0.5),
         annotations = list(list(
           y = 1150,
           xref = 'paper',
           yref = 'y',
           text = 'Required number of subjects',
           showarrow = FALSE
         )))
p

数据:

> dput(test2)
structure(list(month = structure(c(17348, 17379, 17410, 17440, 
17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 
17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262, 
18293, 18322, 18353), class = "Date"), count = c(1L, 2L, 13L, 
10L, 22L, 11L, 18L, 20L, 24L, 16L, 28L, 18L, 16L, 30L, 18L, 21L, 
21L, 21L, 22L, 17L, 24L, 30L, 24L, 17L, 38L, 24L, 23L, 21L, 20L, 
10L, 24L, 16L, 17L, 1L), cumsum = c(1L, 3L, 16L, 26L, 48L, 59L, 
77L, 97L, 121L, 137L, 165L, 183L, 199L, 229L, 247L, 268L, 289L, 
310L, 332L, 349L, 373L, 403L, 427L, 444L, 482L, 506L, 529L, 550L, 
570L, 580L, 604L, 620L, 637L, 638L)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -34L))

> dput(pred)
structure(list(month = structure(c(18383, 18414, 18444, 18475, 
18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748, 
18779, 18809, 18840, 18871, 18901, 18932, 18962, 18993, 19024, 
19052, 19083, 19113, 19144, 19174, 19205, 19236, 19266, 19297, 
19327), class = "Date"), count = c(19, 19, 19, 19, 19, 19, 19, 
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 
19, 19, 19, 19, 19, 19, 19, 19, 19), cumsum = c(657, 676, 695, 
714, 733, 752, 771, 790, 809, 828, 847, 866, 885, 904, 923, 942, 
961, 980, 999, 1018, 1037, 1056, 1075, 1094, 1113, 1132, 1151, 
1170, 1189, 1208, 1227, 1246)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -32L))

输出:

【问题讨论】:

  • 请提供一些虚拟数据(例如使用dput()
  • 我添加了我在代码中使用的两个数据集。

标签: r plotly


【解决方案1】:

这有点棘手。您需要创建一个虚拟索引并为其分配一个ticktext。通常我更喜欢在将两个数据集传递给plot_ly 之前对它们进行行绑定,但我不想过多地改变你的方法。

library(plotly)

test2 <- structure(list(month = structure(c(17348, 17379, 17410, 17440, 
                                            17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 
                                            17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 
                                            18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262, 
                                            18293, 18322, 18353), class = "Date"), count = c(1L, 2L, 13L, 
                                                                                             10L, 22L, 11L, 18L, 20L, 24L, 16L, 28L, 18L, 16L, 30L, 18L, 21L, 
                                                                                             21L, 21L, 22L, 17L, 24L, 30L, 24L, 17L, 38L, 24L, 23L, 21L, 20L, 
                                                                                             10L, 24L, 16L, 17L, 1L), cumsum = c(1L, 3L, 16L, 26L, 48L, 59L, 
                                                                                                                                 77L, 97L, 121L, 137L, 165L, 183L, 199L, 229L, 247L, 268L, 289L, 
                                                                                                                                 310L, 332L, 349L, 373L, 403L, 427L, 444L, 482L, 506L, 529L, 550L, 
                                                                                                                                 570L, 580L, 604L, 620L, 637L, 638L)), class = c("tbl_df", "tbl", 
                                                                                                                                                                                 "data.frame"), row.names = c(NA, -34L))
pred <- structure(list(month = structure(c(18383, 18414, 18444, 18475, 
                                           18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748, 
                                           18779, 18809, 18840, 18871, 18901, 18932, 18962, 18993, 19024, 
                                           19052, 19083, 19113, 19144, 19174, 19205, 19236, 19266, 19297, 
                                           19327), class = "Date"), count = c(19, 19, 19, 19, 19, 19, 19, 
                                                                              19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 
                                                                              19, 19, 19, 19, 19, 19, 19, 19, 19), cumsum = c(657, 676, 695, 
                                                                                                                              714, 733, 752, 771, 790, 809, 828, 847, 866, 885, 904, 923, 942, 
                                                                                                                              961, 980, 999, 1018, 1037, 1056, 1075, 1094, 1113, 1132, 1151, 
                                                                                                                              1170, 1189, 1208, 1227, 1246)), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                              ), row.names = c(NA, -32L))



ticks <- c(as.Date('2017-07-01'), as.Date('2018-07-01'), as.Date('2019-07-01'), as.Date('2020-07-01'), as.Date('2021-07-01'), as.Date('2022-07-01'))
tickDF <- data.frame(index = seq_along(c(test2$month, pred$month)), timestamp = c(test2$month, pred$month))
filteredTicks <- tickDF[tickDF$timestamp %in% ticks,]

hline <- function(y = 0, color = "blue") {
  list(
    type = "line", 
    x0 = 0, 
    x1 = 1, 
    xref = "paper",
    y0 = y, 
    y1 = y, 
    line = list(color = color)
  )
}

p <- plot_ly(mode = 'lines')
p <- p %>%
  add_trace(name = "Cumulative enrollment", type = "scatter", x = seq_along(test2$month), y = test2$cumsum, color = I("blue"), hoverinfo = 'text',
            text = paste('</br> X: ', as.character(format(test2$month, "%B %Y")),
                         '</br> Y: ', test2$cumsum)) %>%
  add_lines(x = seq_along(pred$month)+length(test2$month), y = pred$cumsum, color = I("red"), linetype = I("dash"), name = 'Projected enrollment \n (19 subjects/month)', hoverinfo = 'text',
            text = paste('</br> X: ', as.character(format(pred$month, "%B %Y")),
                         '</br> Y: ', pred$cumsum)) %>%
  layout(xaxis = list(autotick = F, tickmode = "array", tickvals = filteredTicks$index, ticktext = as.character(format(filteredTicks$timestamp, "%B %Y"))),
         yaxis = list(title = "Number of Subjects (Cumulative)", tickmode = "array", tickvals = c(0, 200, 400, 600, 800, 1000, 1109, 1200)), 
         shapes = list(hline(1109)), 
         legend = list(orientation = 'h', xanchor = "center", x = 0.5),
         annotations = list(list(
           y = 1150,
           xref = 'paper',
           yref = 'y',
           text = 'Required number of subjects',
           showarrow = FALSE
         )))
p

hline 函数取自 here。关于cutom hoverinfos,请查看this article

【讨论】:

  • 嗨!非常感谢你的回复。我运行了你提供的代码,我认为它“太多了”。现在,从 2017 年 7 月到 2022 年 12 月,您的代码每个月都会标记。但是,我只想标记(打勾)2017 年 7 月、2018 年 7 月、2019 年 7 月、2020 年 7 月、2021 年 7 月、2022 年 7 月,所以6分。那可能吗?也许我应该更清楚 - 对此感到抱歉!
  • 这是完美的——正是我想要的。非常感谢!
  • 嗨,很抱歉再次询问,但是否可以确保悬停信息显示实际的月份和年份?现在,我认为它正在显示虚拟值。谢谢!
  • 当然,请查看我的答案here 以获取自定义悬停信息。
  • 您好,根据您的回复,我添加了以下内容:p &lt;- plot_ly(mode = 'lines', labels = ~month, parents = NA, values = ~cumsum, hovertemplate = "Date: %{label}&lt;br&gt;Count: %{value}&lt;extra&gt;&lt;/extra&gt;")。但是,我收到以下警告消息: 1:“分散”对象没有这些属性:“标签”、“父母”、“值”。我不确定散点图的等效属性是什么。非常感谢。
【解决方案2】:

您从这里设置自定义值

   plot_ly(df, x = x, y = y) %>%
    layout(xaxis = list(
        range = 
            c(as.numeric(as.POSIXct("2019-01-01", format="%Y-%m-%d"))*1000,
              as.numeric(as.POSIXct("2020-08-31", format="%Y-%m-%d"))*1000),
        type = "date"))

【讨论】:

  • 您好!我编辑了您提供的内容:xaxis = list(range = c(as.numeric(as.POSIXct("2017-07-01", format="%Y-%m-%d"))*1000, as.numeric(as.POSIXct("2022-07-01", format="%Y-%m-%d"))*1000), type = "date"),由于某种原因,它没有任何区别:(
  • 删除此xaxis = list(autotick = F, tickmode = "array", tickvals = c(as.Date('2017-07-01'), as.Date('2018-07-01'), as.Date('2019-07-01'), as.Date('2020-07-01'), as.Date('2021-07-01'), as.Date('2022-07-01')) 并将其分解为更简单的内容。首先尝试一个日期或一个简单的范围。确保正确映射数据框,这里有一个link 来帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 2022-06-30
  • 2012-03-14
  • 2020-03-30
相关资源
最近更新 更多