Highlight weekend using ggplot?

I did not find any historical questions about this ... I want to emphasize the weekend performance for the ggplot graph so that the user can directly identify the graph, which may be a gray shade? right away.

Here is a simple version of the test data:

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)))

Simple version of my graph:

ggplot() + geom_line(aes(x=DATE,y=count),data=test) + labs(title="test")

Thus, the result may be somehow lower ...

enter image description here

+4
source share
4 answers

Here is the code that executes both weekends in your data. You can summarize any number of days off by adding more calls to geom_rect () (or by calling a loop that does).

# your data
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") 

# get all the start and end points
library(lubridate) # for hour function
sats <- which(hour(test$DATE)==0 & weekdays(test$DATE)=='Saturday')
suns <- which(hour(test$DATE)==23 & weekdays(test$DATE)=='Sunday')

# do your plot plus weekend highlights
your_plot +
  geom_rect(aes(xmin=DATE[sats[1]], xmax=DATE[suns[1]],
                  ymin=min(count), ymax=max(count)),
            fill='yellow', alpha=.005) +

  geom_rect(aes(xmin=DATE[sats[2]], xmax=DATE[suns[2]],
                ymin=min(count), ymax=max(count)),
            fill='yellow', alpha=.005)

Here's the output

+3
source

Using geom_area () is more concise and will work for any date range.

    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")

enter image description here

+4

, , , :

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")

. , , .

Resulting image

+1

geom_bar(), , , NA . lubridate, . ggplot(), , y "":

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

, .

0

All Articles