Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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 在ggplot2中为许多不同部分着色_R_Plot_Ggplot2 - Fatal编程技术网

R 在ggplot2中为许多不同部分着色

R 在ggplot2中为许多不同部分着色,r,plot,ggplot2,R,Plot,Ggplot2,这个问题建立在和之上 在我深入讨论细节之前,我希望能够压缩我必须编写的代码量,以便对许多不同的部分进行着色。我希望能够将所有这些信息放在一段代码中 这是我的MWE: library(ggplot2) library(scales) set.seed(1) data <- data.frame(Date = seq(as.Date('2000-01-01'), len= 23, by="1 day"), Valu

这个问题建立在和之上

在我深入讨论细节之前,我希望能够压缩我必须编写的代码量,以便对许多不同的部分进行着色。我希望能够将所有这些信息放在一段代码中

这是我的MWE:

library(ggplot2)
library(scales)
set.seed(1)
data <- data.frame(Date = seq(as.Date('2000-01-01'), 
                   len= 23, by="1 day"), 
                   Value = sample(1:50, 23, replace=TRUE))
rect1 <- data.frame(xmin=as.Date('2000-01-03 12:00:00'), 
                    xmax=as.Date('2000-01-04 12:00:00'), 
                    ymin=-Inf, 
                    ymax=Inf)
rect2 <- data.frame(xmin=as.Date('2000-01-10'), 
                    xmax=as.Date('2000-01-11'), 
                    ymin=-Inf, 
                    ymax=Inf)
rect3 <- data.frame(xmin=as.Date('2000-01-17 00:00:00'), 
                    xmax=as.Date('2000-01-18 00:00:00'), 
                    ymin=-Inf, 
                    ymax=Inf)
ggplot() + 
  geom_line(data=data, aes(x = Date, y = Value)) + 
  geom_rect(data = rect1, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), alpha = 0.4) + 
  geom_rect(data = rect2, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), alpha = 0.4) + 
  geom_rect(data = rect3, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), alpha = 0.4) + 
  scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d"))
因此,我得到了一个有3个不同阴影区域的图:

有3个部分要遮光不是很糟糕,但是如果我需要遮光100呢?有没有办法不定义100个数据帧,然后逐个添加所有这些着色


第二,我希望我的着色在值的中间。但在所有三个规范rect1、rect2和rect3中,每个着色从该日期的值开始,到下一个日期的值结束。有什么建议吗?

你比你想象的更接近了

不必为每个矩形创建一个数据框,您只需创建一个数据框,其中每行都是要着色的日期范围。这里是上面示例的单个数据帧版本,数据与您的相同


dateRanges你比你想象的要近

不必为每个矩形创建一个数据框,您只需创建一个数据框,其中每行都是要着色的日期范围。这里是上面示例的单个数据帧版本,数据与您的相同


DATRANGES

可以使用PosixCT对象在中间绘制阴影。

可以包含任意数量的阴影,以定义包含所有坐标的列表

library(ggplot2)
library(scales)
set.seed(1)
data <- data.frame(
  Date = as.POSIXct(seq(as.Date('2000-01-01'), len= 23, by="1 day")), 
  Value = sample(1:50, 23, replace=TRUE)
)
您可以定义一个包含所有开始日期的向量,假设带宽始终为一天。或者,您可以定义包含这两个日期的数据帧,并使用plyr中的apply或dlply

vectorDates <-seq(
  as.POSIXct('2000-01-03 12:00:00'),
  as.POSIXct('2000-01-20 12:00:00'),
  length.out = 10
)
list_geom_rect <- lapply(vectorDates, function(date1)
{
  date2 <- date1 + 3600 * 24
  rect <- data.frame(
    xmin = date1, xmax = date2, 
    ymin = -Inf,  ymax = Inf
  )
  geom_rect(
    data = rect,
    mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
    alpha = 0.4
  )
})
现在可以构建绘图并添加所有标注栏

 p <- ggplot() + geom_line(data=data, aes(x = Date, y = Value))
    for(geom in list_geom_rect){
      p <- p + geom
    }
 p

你可以用PosixCT对象在中间绘制阴影。

可以包含任意数量的阴影,以定义包含所有坐标的列表

library(ggplot2)
library(scales)
set.seed(1)
data <- data.frame(
  Date = as.POSIXct(seq(as.Date('2000-01-01'), len= 23, by="1 day")), 
  Value = sample(1:50, 23, replace=TRUE)
)
您可以定义一个包含所有开始日期的向量,假设带宽始终为一天。或者,您可以定义包含这两个日期的数据帧,并使用plyr中的apply或dlply

vectorDates <-seq(
  as.POSIXct('2000-01-03 12:00:00'),
  as.POSIXct('2000-01-20 12:00:00'),
  length.out = 10
)
list_geom_rect <- lapply(vectorDates, function(date1)
{
  date2 <- date1 + 3600 * 24
  rect <- data.frame(
    xmin = date1, xmax = date2, 
    ymin = -Inf,  ymax = Inf
  )
  geom_rect(
    data = rect,
    mapping = aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
    alpha = 0.4
  )
})
现在可以构建绘图并添加所有标注栏

 p <- ggplot() + geom_line(data=data, aes(x = Date, y = Value))
    for(geom in list_geom_rect){
      p <- p + geom
    }
 p

谢谢@anandthakker,这很有效。关于你的第1点的一个问题-xmin=from-1实际上是两天而不是一天。如果我只想遮阴一天,但把它移到这一天的中心会怎么样。例如,对于03年1月,我会像你一样拥有酒吧,但你的酒吧大小是1月03日的一半,仍然居中。实际上,我会从1月2日中午到1月3日中午。另一个答案是这样的,但是不使用POSIXct类也能做到吗?解决了-使用xmin=from-.5和xmax=from+.5。谢谢@anandthakker这是可行的。关于你的第1点的一个问题-xmin=from-1实际上是两天而不是一天。如果我只想遮阴一天,但把它移到这一天的中心会怎么样。例如,对于03年1月,我会像你一样拥有酒吧,但你的酒吧大小是1月03日的一半,仍然居中。实际上,我会从1月2日中午到1月3日中午。另一个答案是这样的,但是不使用POSIXct类是否可以做到这一点呢?解决了这个问题-使用xmin=from-.5和xmax=from+.5。