2017-07-14 45 views
0

我有点卡住了一些代码。当然,我会欣赏这样一段代码,它可以排序我的困境,但我也很感激如何排除这种情况。在R Studio中每小时绘制一个小时数据点(每天1个),每小时点数(每天24个)

这里所说:所有的 首先,我安装的软件包(GGPLOT2,lubridate和openxlsx)

相关部分: 我提取来自意大利天然气TSO网站文件:

Storico_G1 <- read.xlsx(xlsxFile = "http://www.snamretegas.it/repository/file/Info-storiche-qta-gas-trasportato/dati_operativi/2017/DatiOperativi_2017-IT.xlsx",sheet = "Storico_G+1", startRow = 1, colNames = TRUE) 

然后,我创建了变量的数据帧我想保留:

​​

然后更改时间格式:

Storico_G1_df$pubblicazione <- ymd_h(Storico_G1_df$Storico_G1.pubblicazione) 

现在斗争开始了。由于在这个例子中,我想用2个不同的Y轴来绘制2个时间序列,因为范围是非常不同的。这不是一个真正的问题,因为有了融化函数和ggplot,我可以实现这一点。但是,由于1栏中有NAs,我不知道如何解决这个问题。由于在不完整(SAS)专栏中,我主要关心16点的数据点,理想情况下我会在一张图表上每小时绘制一个图表,并且在第二个图表上(每天16点)每天只有1个数据点。我附加了一个无关的图表样式,我的意思是图表样式。但是,在附图中,我在两个图表上都有相同的数据点,因此它工作正常。

enter image description here

感谢有任何提示。

保重

回答

0
library(lubridate) 
library(ggplot2) 
library(openxlsx) 
library(dplyr) 

#Use na.strings it looks like NAs can have many values in the dataset 
storico.xl <- read.xlsx(xlsxFile = "http://www.snamretegas.it/repository/file/Info-storiche-qta-gas-trasportato/dati_operativi/2017/DatiOperativi_2017-IT.xlsx", 
         sheet = "Storico_G+1", startRow = 1, 
         colNames = TRUE, 
         na.strings = c("NA","N.D.","N.D")) 

#Select and rename the crazy column names 
storico.g1 <- data.frame(storico.xl) %>% 
    select(pubblicazione, IMMESSO, SBILANCIAMENTO.ATTESO.DEL.SISTEMA..SAS.) 
names(storico.g1) <- c("date_hour","immesso","sads") 


# the date column look is in the format ymd_h 
storico.g1 <- storico.g1 %>% mutate(date_hour = ymd_h(date_hour)) 


#Not sure exactly what you want to plot, but here is each point by hour 
ggplot(storico.g1, aes(x= date_hour, y = immesso)) + geom_line() 

#For each day you can group, need to format the date_hour for a day 
#You can check there are 24 points per day 
#feed the new columns into the gplot 

storico.g1 %>% 
    group_by(date = as.Date(date_hour, "d-%B-%y-")) %>% 
    summarise(count = n(), 
      daily.immesso = sum(immesso)) %>% 
    ggplot(aes(x = date, y = daily.immesso)) + geom_line() 
+0

感谢您的帮助!我的困难是我想用频繁的数据(如果我每天只对16点数据点过滤1点)绘制频繁的数据(每天24小时数据点)。如果我忽视了提示,我提前道歉。 –