Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highlight weekends using ggplot?

Tags:

r

ggplot2

I haven't found any history questions about this... I want to highlight the weekend performance for a ggplot graph, so that user could tell directly one the graph which part(may be a gray shade?) is weekend performance right away.

Here's a simple version of testing 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 is:

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

So that result could be like something below...

enter image description here

like image 501
linp Avatar asked Aug 13 '14 15:08

linp


1 Answers

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

like image 98
mynameisthis Avatar answered Sep 23 '22 12:09

mynameisthis