简体   繁体   English

创建一个在x轴和y轴上具有时间的图(以R为单位)

[英]Create a plot with time on the x- and y-axis (in R)

There are two points in time (two measurements), I would like to plot the first time on the x-axis and the second on the y-axis as points, hence I could easily inspect data-points above the 45°-line for which the second measurement is after the first time (true for most, but not all cases) and evaluate the time elapsed from the first moment. 有两个时间点(两次测量),我想将第一次在x轴上绘制,将第二次在y轴上绘制为点,因此我可以轻松检查45°线以上的数据点,第二次测量是在第一次之后(对于大多数情况,但不是所有情况都为真),并评估从第一时刻起经过的时间。

I therefore have two columns with dates, I can transform them to date or date-time objects. 因此,我有两列带有日期,可以将它们转换为datedate-time对象。 I cannot eastimate right now, which format may allow an easier implemention of the plot. 我现在无法确定,哪种格式可以使情节的实现更容易。

D = data.frame(time1 = c("2007-06-22","2007-05-22","2007-05-23"), time2 = c("2007-06-22","2007-05-24","2007-06-05"))
D$time1 <- strptime(D$time1, format = "%Y-%m-%d")
D$time2 <- strptime(D$time2, format = "%Y-%m-%d")
class(D$time1)
D$time1 <- as.Date(D$time1, format = "%Y-%m-%d")
class(D$time1)

I need something as easy as: 我需要一些简单的东西:

plot(D$time1, D$time2)

But I also need a modifiable version, which does not plot the days, but allows for breaks (weeks, month, years), say month. 但是我还需要一个可修改的版本,该版本不绘制日期,而是允许休息(周,月,年),例如月份。

在此处输入图片说明

Thank you so much for ideas. 非常感谢您的想法。

Solution : Both versions worked in principle. 解决方案 :两种版本原则上都可以工作。 I've chosen the ggplot because one's more flexible on labels etc. I did the data-cleaning in advance, so after subsetting missing data, I used: 我之所以选择ggplot是因为它在标签等方面更灵活。我事先进行了数据清理,因此在对丢失的数据进行ggplot之后,我使用了:

library( ggplot2 )
ggplot( mydata, aes(x=t0, y=t1) ) + geom_point() +
  scale_x_date(date_labels = "%b %y", date_breaks = "3 month") +
  scale_y_date(date_labels = "%b %y", date_breaks = "3 month") 

3 month are a good break for my 2 years of data. 3个月对我2年的数据来说是一个不错的休息时间。 Here's my solution: 这是我的解决方案:

在此处输入图片说明

You could do something like this: 您可以执行以下操作:

dates_axis <- as.Date(strptime(c("2007-01-01","2007-12-01"), format = "%Y-%m-%d"))
dates_axis <- seq.Date(dates_axis[1],dates_axis[2],"month")

oldpar <- par()
par(mar = c(6,6,1,1))
plot(dates_axis,dates_axis,
    xaxt = "n",
    yaxt = "n",
    bty = "n",
    type = "n",
    xlab = "",
    ylab = "")
axis(1, at = dates_axis, labels = months(dates_axis), las = 2)
axis(2, at = dates_axis, labels = months(dates_axis), las = 1)
points(D$time1, D$time2, pch = 20)
abline(a = 0, b = 1)
par(oldpar)

在此处输入图片说明

ggplot understands time/date data. ggplot了解时间/日期数据。 Use the Date class, as you've done in the question: 就像在问题中一样,使用Date类:

D <- data.frame( time1 = as.Date(c("2007-06-22","2007-05-22","2007-05-23")),
                time2 = as.Date(c("2007-06-22","2007-05-24","2007-06-05")) )

Now you can pass it to ggplot , using scale_x_date and scale_y_date to achieve flexibility in specifying breaks/labels/etc.: 现在,您可以使用scale_x_datescale_y_date将其传递给ggplot ,以灵活地指定breaks / labels / etc:

library( ggplot2 )
ggplot( D, aes(x=time1, y=time2) ) + geom_point() +
    scale_x_date( date_labels = "%b %d", date_breaks = "1 week") +
    scale_y_date( date_labels = "%b %d", date_breaks = "1 day" )

See ?scale_x_date for more information. 有关更多信息,请参见?scale_x_date

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM