R - ggplot2 - 如何在POSIX轴上使用限制? [英] R - ggplot2 - How to use limits on POSIX axis?

查看:202
本文介绍了R - ggplot2 - 如何在POSIX轴上使用限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操纵POSIX以在ggplot轴中使用的最明智的方法是什么?

我正在尝试创建一个函数,用于绘制横跨周期的许多图形(每天一个),对x轴使用POSIX时间。



为此,我创建了一个额外的整数列 DF $ Day 那天,我输入了这个函数。然后,我使用那天创建一个子集,我使用ggplot2进行绘图。我想到如何使用 scale_x_datetime 来格式化POSIX x轴。基本上,我有它显示小时和放大器。只有分钟,省略日期。



以下是我的问题:如何在一天中的几小时内为每个图表设置限制?

以下是一些有用的,可重复的代码来获得想法。它创建了第一天,显示它3秒&收益创造第二天。但是,每天的限制是根据时间变量的范围来选择的。例如,我怎样才能使范围,整天(0小时 - 24小时)?

  DF < -  data.frame(matrix(ncol = 0,nrow = 4))

DF $ time< - as.POSIXct(c(2010-01-01 02:01:00,2010-01-01 18:10:00,2010-01-02 04:20:00 ,2010-01-02 13:30:00))
DF $ observation <-c(1,2,1,2)
DF $ Day <-c(1, (个人日)在1:2){
Day_subset < - DF [DF $ Day == as.integer(Individual_Day),]
($ 1,2)


print(ggplot(data = Day_subset,aes_string(x =time,y =observation))+ geom_point()+
scale_x_datetime(breaks =(2小时),minor_breaks =( 1小时),labels = date_format(%H:%M)))

Sys.sleep(3)}
$ b

解决方案

好的,这里有一种方法。 #...
for(Individual_Day in 1:2){
Day_subset < - DF [DF $ Day == as.integer(Individual_Day),]
较低( - (Day_subset,as.POSIX ct(strftime(min(time),%y-%m-%d)))
upper< - with(Day_subset,as.POSIXct(strftime(as.Date(max(time))+ 1,%Y-%m-%d)) - 1)
limits = c(lower,upper)

print(ggplot(data = Day_subset,aes(x = time ,y =观察值))+
geom_point()+
scale_x_datetime(breaks =(2小时),
minor_breaks =(1小时),
labels = date_format (%H:%M),
限制=限制)

}



计算对于 lower 取得子集中的最小时间,并强制它只带日期部分(例如,去掉时间部分)。转换回POSIXct会生成当天的开始。



计算 upper 稍微复杂一点。您必须将最大时间转换为日期值并加1(例如1天),然后转换为字符(剥离时间部分),转换回POSIXct并减1(例如1秒)。这会在结束的日子里产生23:59。



这么小的事情需要大量的工作。我希望别人发布一个更简单的方法来做到这一点......


What is the smartest way to manipulate POSIX for use in ggplot axis?

I am trying to create a function for plotting many graphs (One per day) spanning a period of weeks, using POSIX time for the x axis.

To do so, I create an additional integer column DF$Day with the day, that I input into the function. Then, I create a subset using that day, which I plot using ggplot2. I figured how to use scale_x_datetime to format the POSIX x axis. Basically, I have it show the hours & minutes only, omitting the date.

Here is my question: How can I set the limits for each individual graph in hours of the day?

Below is some working, reproducible code to get an idea. It creates the first day, shows it for 3 seconds & the proceeds to create the second day. But, each days limits is chosen based on the range of the time variable. How can I make the range, for instance, all day long (0h - 24h)?

DF <- data.frame(matrix(ncol = 0, nrow = 4))

DF$time <- as.POSIXct(c("2010-01-01 02:01:00", "2010-01-01 18:10:00", "2010-01-02 04:20:00", "2010-01-02 13:30:00"))
DF$observation <- c(1,2,1,2)
DF$Day <- c(1,1,2,2)

for (Individual_Day in 1:2) { 
Day_subset <- DF[DF$Day == as.integer(Individual_Day),]

print(ggplot( data=Day_subset, aes_string( x="time", y="observation") ) + geom_point() +
      scale_x_datetime( breaks=("2 hour"), minor_breaks=("1 hour"), labels=date_format("%H:%M")))

Sys.sleep(3) }

解决方案

Well, here's one way.

# ...
for (Individual_Day in 1:2) { 
Day_subset <- DF[DF$Day == as.integer(Individual_Day),]
lower <- with(Day_subset,as.POSIXct(strftime(min(time),"%Y-%m-%d")))
upper <- with(Day_subset,as.POSIXct(strftime(as.Date(max(time))+1,"%Y-%m-%d"))-1)
limits = c(lower,upper)

print(ggplot( data=Day_subset, aes( x=time, y=observation) ) + 
        geom_point() +
        scale_x_datetime( breaks=("2 hour"), 
                          minor_breaks=("1 hour"), 
                          labels=date_format("%H:%M"),
                          limits=limits)
      )
}

The calculation for lower takes the minimum time in the subset and coerces it to character with only the date part (e.g., strips away the time part). Converting back to POSIXct generates the beginning of that day.

The calculation for upper is a little more complicated. You have to convert the maximum time to a Date value and add 1 (e.g., 1 day), then convert to character (strip off the time part), convert back to POSIXct, and subtract 1 (e.g., 1 second). This generates 23:59 on the end day.

Huge amount of work for such a small thing. I hope someone else posts a simpler way to do this...

这篇关于R - ggplot2 - 如何在POSIX轴上使用限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆