ggplot2 per year

451 views Asked by At

I have a dataframe which contains productivity measure by date. the date starts from 05/04/2013 to 07/11/2014. The productivity measure is for 6 organisational unit

Data looks like this:

> str(prods)
'data.frame':   588 obs. of  6 variables:
 $ SOM  : Factor w/ 7 levels "BVG1 Scotland",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Skill: Factor w/ 1 level "A": 1 1 1 1 1 1 1 1 1 1 ...
 $ Date : Date, format: "2013-04-05" "2013-04-12" "2013-04-19" "2013-04-26" ...
 $ Prod : num  2.97 2.94 2.87 2.95 2.97 2.97 2.97 2.94 2.86 2.98 ...
 $ Month: num  4 4 4 4 5 5 5 5 5 6 ...
 $ Year : num  2013 2013 2013 2013 2013 ...

Summary:

> summary(prods)
                           SOM     Skill        Date                 Prod           Month            Year     
 BVG1 Scotland               :84   A:588   Min.   :2013-04-05   Min.   :2.370   Min.   : 1.00   Min.   :2013  
 BVG11 Highlands & IslandsA  :84           1st Qu.:2013-08-28   1st Qu.:2.888   1st Qu.: 4.75   1st Qu.:2013  
 BVG12 North East ScotlandA  :84           Median :2014-01-20   Median :3.030   Median : 7.00   Median :2014  
 BVG13 Central ScotlandA     :84           Mean   :2014-01-20   Mean   :3.029   Mean   : 6.75   Mean   :2014  
 BVG14 South East ScotlandA  :84           3rd Qu.:2014-06-14   3rd Qu.:3.180   3rd Qu.: 9.00   3rd Qu.:2014  
 BVG15 West Central ScotlandA:84           Max.   :2014-11-07   Max.   :3.510   Max.   :12.00   Max.   :2014  
 BVG16 South West ScotlandA  :84                                                                              

I simply want draw a line plot using ggplot2 showing 2013 and 2014 "Prod" I can draw Actual prod to date as show below: enter image description here

The code I used is:

ggplot(prods, aes(x = Date, y = Prod, group = SOM)) + 
     geom_line(lwd = 1.3, colour = "red") +
     ylab("Actual Productivity") + theme(axis.title.x=element_blank()) +
     facet_wrap(~ SOM)

It would be nice to show prod by year i.e. 2013 line with purple color and 2014 line with red for example.

any help would be highly appreciated.

regards,

This is what I did:

ggplot(prods[Year==2013,], aes(x = Date, y = Prod, group = SOM)) + 
 geom_line(lwd = 1.3, colour = "purple") +
 ylab("Actual Productivity") + theme(axis.title.x=element_blank()) +
 geom_line(data=prods[Year==2014,], aes(x = Date, y = Prod, group = SOM),lwd = 1.3, colour = "red") +
        scale_color_manual("Period", values = c("purple","red"), labels = c("2013","2014")) +
 facet_wrap(~ SOM)

I am not getting any error but there is no legend popping up on the image. It looks like this: enter image description here

1

There are 1 answers

4
user1317221_G On
 prods <- data.frame(Prod=c(1:10,1:10),
                     Date=rnorm(20),
                     year=rep(c(2013,2014,2013,2014),each=5),
                     SOM=rep(letters[1:2],each=10))

above makes fake data then code below subsets on year. you don't have to make a year column like i did you could subset on your actual data column itself. The below plots once for 2013 and once for 2014.

ggplot(prods[prods$year==2013,], aes(x = Date, y = Prod, group = SOM)) + 
 geom_line(lwd = 1.3, colour = "purple") +
 geom_line(data=prods[prods$year==2014,], 
           aes(x = Date, y = Prod, group = SOM),lwd = 1.3, colour = "red") +
 ylab("Actual Productivity") + 
 theme(axis.title.x=element_blank()) +
 facet_wrap(~ SOM)

enter image description here