在R中:当使用posixct作为日期 - 时间列时,如何消除日期 - 时间序列图中的间隙?

时间:2021-12-31 16:00:20

This is a problem for which i cannot find any help online.

这是一个我在网上找不到任何帮助的问题。

Here is the link to the original data (too large to paste here): https://drive.google.com/open?id=0B5KaRU02aEnAbnM5ODNPMkYxN0E

以下是原始数据的链接(此处无法粘贴):https://drive.google.com/open?id = 0B5KaRU02aEnAbnM5ODNPMkYxN0E

and here is its structure

这是它的结构

> str(dfsite)
'data.frame':   11712 obs. of  8 variables:
 $ Site_ID      : chr  "02AB08-001" "02AB08-001" "02AB08-001" "02AB08-001" ...
 $ Deployment_ID: num  15352 15352 15352 15352 15352 ...
 $ RecDate      : chr  "01-07-2007" "01-07-2007" "01-07-2007" "01-07-2007" ...
 $ RecTime      : chr  "00:00:00" "00:30:00" "01:00:00" "01:30:00" ...
 $ Temperature  : num  19.5 19.5 19.6 19.6 19.6 ...
 $ TimeStep     : num  30 30 30 30 30 30 30 30 30 30 ...
 $ RecYr        : Factor w/ 19 levels "1996","1997",..: 12 12 12 12 12 12 12 12 12 12 ...
 $ RecDT        : POSIXct, format: "2007-07-01 00:00:00" "2007-07-01 00:30:00" "2007-07-01 01:00:00" "2007-07-01 01:30:00" ...

The RecDT column consists of daily data from 00:00:00 to 23:30:00 for the entire month of July for many years: 1) 2007-07-01 00:00:00-23:30:00 to 2007-07-30 00:00:00-23:30:00 2) 2009-07-01 00:00:00-23:30:00 to 2009-07-30 00:00:00-23:30:00 ...

RecDT栏包含7月份整个7月00:00:00至23:30:00的每日数据:1)2007-07-01 00:00:00-23:30:00至2007- 07-30 00:00:00-23:30:00 2)2009-07-01 00:00:00-23:30:00至2009-07-30 00:00:00-23:30:00 ..

I would like to create a time series plot for each year which shows day 1 to day 30 of July for a specific site_id.

我想为每年创建一个时间序列图,显示特定site_id的7月1日到30日。

My current code creates a time series plot -for all the months but it should only have the month of July -for all the years but it should create facets for different years

我目前的代码创建了一个时间序列图 - 对于所有月份而言它应该只有七月 - 这些年来它应该创建不同年份的方面

# original data
df <- excel file
# extract year only
df["RecYr"] <- substr(df$RecDate,7,10)
df$RecYr <- as.factor(df$RecYr)

#extract date and time
df["RecDT"] <- paste(df$RecDate,df$RecTime)
df$RecDT <- as.character(df$RecDT)
df$RecDT <- as.POSIXct(df$RecDT,"%d-%m-%Y %H:%M:%S", tz="UTC")

# select specific site
dfsite <- df[df$Site_ID == "02AB08-001",]

# import ggplot2 and create plot
library(ggplot2)
p <- ggplot(dfsite, aes(x=dfsite$RecDT, y=dfsite$Temperature))  + 
  geom_point() +
print(p)

here is my plot

这是我的情节

Thank you very much in advance.

非常感谢你提前。

1 个解决方案

#1


0  

A simple solution could be:

一个简单的解决方案是:

x <- cumsum(table(dfsite$RecYr))
x <- cbind(c(1,x[-length(x)]),x)
xseq <- c(apply(x,1,function(kk) round(seq(kk[1],kk[2],length.out=3))))

p <- ggplot(dfsite, aes(x=1:nrow(dfsite), y=Temperature))  + 
  geom_point() + facet_grid(.~RecYr, scale="free_x") +
  scale_x_continuous("Time", breaks=xseq, 
        labels = dfsite$RecDT[xseq])+
theme(axis.text.x=element_text(angle=45,hjust=1))
print(p)

在R中:当使用posixct作为日期 - 时间列时,如何消除日期 - 时间序列图中的间隙?

#1


0  

A simple solution could be:

一个简单的解决方案是:

x <- cumsum(table(dfsite$RecYr))
x <- cbind(c(1,x[-length(x)]),x)
xseq <- c(apply(x,1,function(kk) round(seq(kk[1],kk[2],length.out=3))))

p <- ggplot(dfsite, aes(x=1:nrow(dfsite), y=Temperature))  + 
  geom_point() + facet_grid(.~RecYr, scale="free_x") +
  scale_x_continuous("Time", breaks=xseq, 
        labels = dfsite$RecDT[xseq])+
theme(axis.text.x=element_text(angle=45,hjust=1))
print(p)

在R中:当使用posixct作为日期 - 时间列时,如何消除日期 - 时间序列图中的间隙?