R 使用ggplot突出周末?

R 使用ggplot突出周末?,r,ggplot2,R,Ggplot2,我没有发现任何关于这个的历史问题。。。我想突出显示ggplot图的周末性能,这样用户就可以直接从图中分辨出哪个部分(可能是灰色阴影?)是周末性能 以下是测试数据的简单版本: test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100))) 所以这个结果可能会像下

我没有发现任何关于这个的历史问题。。。我想突出显示ggplot图的周末性能,这样用户就可以直接从图中分辨出哪个部分(可能是灰色阴影?)是周末性能

以下是测试数据的简单版本:

test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))
所以这个结果可能会像下面这样


根据我的评论,希望这就是您想要的:

test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))

ggplot() + geom_rect(aes(xmin = as.POSIXct("2014-07-26 00:00"), xmax = as.POSIXct("2014-07-28 00:00"), ymin = 0, ymax = 100), fill = "yellow")+
 geom_line(aes(x=DATE,y=count),data=test) + labs(title="test")

test以下是在数据中同时执行两个周末的代码。通过添加更多的geom_rect()调用(或调用一个循环),可以将其推广到任意数量的周末

#您的数据
使用geom_area()进行测试更简洁,适用于任何日期范围

    library(ggplot2)

    test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), 
                                to = as.POSIXct("2014-07-30 00:00"), 
                                by = "hour"),
                       count=floor(runif(336,1,100)))

    test$weekend <- weekdays(test$DATE) %in% c("Saturday", "Sunday")

    ggplot(data=test, aes(x=DATE, y=count)) +
      geom_area(aes(y=weekend*max(count)), fill="yellow") +
      geom_line() +
      labs(title="test")
库(ggplot2)

测试我发现了一个很好的解决方案,使用
geom_bar()
创建一个周末无限的向量,剩余的值为NA,不会绘制。我使用了
lubridate
,因为我对它比较熟悉。我还必须在对
ggplot()
的第一次调用中命名美学,以便轴继承其专有名称,并且
y
不标记为“weekend”:

库(lubridate)
图书馆(GG2)

测试这里是一种使用不同工具的方法,更精确地说。这样做的好处是,您不必精确定义
x
边界

library(dplyr)
library(lubridate)
library(ggplot2)

test %>%
  # My preference :-)
  rename_all(tolower) %>%
  # Computing the weekends by converting in weekday starting on monday to ease the cut
  mutate(weekend = wday(date, week_start = getOption("lubridate.week.start", 1)) > 5) %>%
  ggplot() +
  geom_line(aes(x = date, y = count)) +
  # And here is the trick!
  scale_fill_manual(values = c("alpha", "yellow")) +
  geom_tile(aes(
    x = date,
    y = min(count),
    height = Inf,
    fill = weekend
  ), alpha = .4)


注意:我已经写了一篇关于这个主题的更详细的文章。

See?。你应该做你想做的事。也可能有帮助。@AdamKimberley很好,谢谢!我实际上可以指定日期而不是单元格位置。我认为我需要在更长的时间范围内想出一个更聪明的方法,但到目前为止,这就像一个魅力美好的我只是按照亚当的建议指定了日期,但这正是我想要的关于更长时间范围的建议。谢谢!:)提到lubridate包中的小时和工作日函数如何?很好。补充。(虽然只是小时。工作日在基数中)您也可以使用
data.table::hour
如果要使用循环添加更多geom_rect(),请不要忘记使用aes_string()而不是普通的aes()。否则,将只显示最后一个矩形。我无法将其用于刻面绘图,但我在这里找到了一条有用的线索:
    library(ggplot2)

    test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), 
                                to = as.POSIXct("2014-07-30 00:00"), 
                                by = "hour"),
                       count=floor(runif(336,1,100)))

    test$weekend <- weekdays(test$DATE) %in% c("Saturday", "Sunday")

    ggplot(data=test, aes(x=DATE, y=count)) +
      geom_area(aes(y=weekend*max(count)), fill="yellow") +
      geom_line() +
      labs(title="test")
library(lubridate)
library(ggplot2)

test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))
your_plot <- ggplot(test) + geom_line(aes(x=DATE,y=count)) + labs(title="test") 
your_plot

test$wd <- wday(test$DATE, label = TRUE)
test$weekend[test$wd=="Sat" | test$wd=="Sun"] <- 1/0

highlight_plot <- ggplot(test, aes(x= DATE, y = weekend)) + 
  geom_bar(stat="Identity", aes(y=weekend), col="yellow", alpha=.4, width = 1) + 
geom_line(aes(x=DATE,y=count)) + labs(title="test") 

highlight_plot
library(dplyr)
library(lubridate)
library(ggplot2)

test %>%
  # My preference :-)
  rename_all(tolower) %>%
  # Computing the weekends by converting in weekday starting on monday to ease the cut
  mutate(weekend = wday(date, week_start = getOption("lubridate.week.start", 1)) > 5) %>%
  ggplot() +
  geom_line(aes(x = date, y = count)) +
  # And here is the trick!
  scale_fill_manual(values = c("alpha", "yellow")) +
  geom_tile(aes(
    x = date,
    y = min(count),
    height = Inf,
    fill = weekend
  ), alpha = .4)