仅在第一个轴壁虱或更改上展示年份

发布于 2025-01-18 02:38:16 字数 1089 浏览 2 评论 0原文

我有一个关于 R 中日期轴格式的问题。

我想在 x 轴上指定自定义日期刻度格式,如下所示: '%a %e %b \n %Y' 但我只想在第一个 x 轴刻度上显示年份。因为对每个刻度标签重复它太奇怪了。

我有以下小示例:

dt <- data.frame(
  x = as.Date(c('2013-02-04', '2013-04-05', '2013-06-06', '2013-08-07', '2013-10-02')),
  y = c(1,4,5,6,2)
)
plot_ly(data = dt, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
  layout(xaxis = list(tickformat = '%a %e %b \n %Y'))

在此处输入图像描述

这与此 javascript 示例 它确实有效!

输入图片此处描述

这篇使用 python 的文章似乎也有效。所以我现在不确定我在 R 中做错了什么?或者如果这是一个错误?


编辑:我想避免使用 ggplotly 并直接使用plotly,因为这提供了更好的控制。

I have a question regarding axis formatting of dates in R plotly.

I want to specify custom date tickformats on my x-axis as follows: '%a %e %b \n %Y' But I only want to show the year on the first xaxis tick. As it would be too weird to repeat it for each tick label.

I have the following small example:

dt <- data.frame(
  x = as.Date(c('2013-02-04', '2013-04-05', '2013-06-06', '2013-08-07', '2013-10-02')),
  y = c(1,4,5,6,2)
)
plot_ly(data = dt, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
  layout(xaxis = list(tickformat = '%a %e %b \n %Y'))

enter image description here

Which is the exact same setup from this javascript example where it does work!

enter image description here

This post with python also seems to work. So I am not sure now what I am doing wrong in R? Or if this is a bug?


Edit: I would like to avoid using ggplotly and directly work with plotly as this provide greater control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文