如何调整ggplot直方图的时间刻度轴

发布于 2024-08-24 05:58:27 字数 354 浏览 6 评论 0原文

我正在使用一个数据框,其中一列由 POSIXct 日期时间值组成。我正在尝试使用 ggplot2 绘制这些时间戳的直方图,但遇到两个问题:

  1. 我不知道如何在 geom_histogram() 中设置 binwidth >。我想将每个垃圾箱设置为一天或一周。我尝试提供 difftime 对象,但出现错误。我也尝试过 binwidth=1 但 R 只是挂起。

  2. 如何在 scale_x_time() 中设置限制?我可以让它工作的唯一方法是使用 as.Date() 转换我的 POSIXct 时间戳。

I am working with a data frame where one of the columns consists of POSIXct date-time values. I am trying to plot a histogram of these timestamps using ggplot2 but I'm having two issues:

  1. I don't know how to set the binwidth in geom_histogram(). I'd like to set each bin to a day or a week. I've tried providing a difftime object, but I get an error. I also tried binwidth=1 but R just hangs.

  2. How do I set the limits in scale_x_time()? The only way I could get it to work was by converting my POSIXct timestamps using as.Date().

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-08-31 05:58:27
  1. binwidth 以秒为单位测量,因此每周设置 binwidth=7*24*60*60。
  2. 限制可以作为 2 个 POSIXct 对象的向量给出。

一个例子:

y<-as.POSIXct('1970/01/01')+cumsum(rnorm(100,mean=24*60*60,sd=24*60*60))
p<-qplot(y,binwidth=7*24*60*60,fill=I('steelblue'),col=I('black'))
p<-p+scale_x_datetime(major="1 week",
                      minor="1 days",
                      format="%e/%m/%Y",
                      limits=c(as.POSIXct('1970/02/01'),
                               as.POSIXct('1970/03/31')))
print(p)
  1. The binwidth is measured in seconds, so to bin per week set binwidth=7*24*60*60.
  2. Limits can be given as a vector of 2 POSIXct objects.

An example:

y<-as.POSIXct('1970/01/01')+cumsum(rnorm(100,mean=24*60*60,sd=24*60*60))
p<-qplot(y,binwidth=7*24*60*60,fill=I('steelblue'),col=I('black'))
p<-p+scale_x_datetime(major="1 week",
                      minor="1 days",
                      format="%e/%m/%Y",
                      limits=c(as.POSIXct('1970/02/01'),
                               as.POSIXct('1970/03/31')))
print(p)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文