Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 调整不同的时间序列_R_Ggplot2 - Fatal编程技术网

R 调整不同的时间序列

R 调整不同的时间序列,r,ggplot2,R,Ggplot2,我试图“捕捉”酒吧里的一些点。 这些点代表3年内每月36个值。 条形图表示同一3年内每年的3个值 如果您运行代码,您可以看到第一年的某些点可能会被第二年的条捕获,而第三年的点可能会“耗尽”最后一条 如何对齐条和点 library(ggplot2) set.seed(1) df.year <- data.frame(yeardate = seq(as.Date("2010-01-01"), by = "year", length.out = 3), datevalue = abs(rnor

我试图“捕捉”酒吧里的一些点。 这些点代表3年内每月36个值。 条形图表示同一3年内每年的3个值

如果您运行代码,您可以看到第一年的某些点可能会被第二年的条捕获,而第三年的点可能会“耗尽”最后一条

如何对齐条和点

library(ggplot2)

set.seed(1)
df.year <- data.frame(yeardate = seq(as.Date("2010-01-01"), by = "year", length.out = 3), datevalue = abs(rnorm(3)))
df.month <- data.frame(monthdate = seq(as.Date("2010-01-01"), by = "month", length.out = 36), datevalue = abs(rnorm(36)))
df.month$inyear <- format(df.month$monthdate, "%Y") 

df.month
p <- ggplot()
p <- p +    geom_point(
            data = df.month
            ,aes(x = monthdate, y = datevalue, color=inyear)
        )
p <- p +    geom_bar(
            data = df.year
            ,aes(x = yeardate, y = datevalue)
            ,alpha=0.7 
            ,stat = "identity"
        )           
p + scale_x_date(labels = date_format("%Y"), breaks = date_breaks("years"))
库(ggplot2)
种子(1)

df.year
geom_条形图
将条形图置于给定日期的中心。由于给定的日期是一年中的第一天,因此它以一年中的第一天为中心,因此2012年的大部分时间不在以2012-01-01为中心的时间段内(而该时间段的大部分时间在2011年)。因此,在年年中任何一个中心都是:
df.year <- data.frame(yeardate = seq(as.Date("2010-07-01"), 
                                     by = "year", 
                                     length.out = 3), 
                      datevalue = abs(rnorm(3)))
给予

df.year <- data.frame(yearstart = seq(as.Date("2010-01-01"), 
                                      by = "year", length.out = 3),
                      yearend = seq(as.Date("2010-12-31"), 
                                    by = "year", length.out = 3),
                      datevalue = abs(rnorm(3)))
p <- p +    geom_rect(
            data = df.year
            ,aes(xmin = yearstart, xmax = yearend,
                 ymin = 0, ymax = datevalue)
            ,alpha=0.7
        )