Graph graph month-year graph and filled in for a year; with data input as char date

Environment: Win 7 HP, R v2.15.1

What I want to achieve:

  • Plot y (numeric) vs x (date) with
  • labels month abbreviation, sorted mon + year, las2 vertically.
  • colors filled per year
  • facet grid by years

I tried different approaches after reading different topics in this forum, but could not get what I need. Help is needed. Attach sample data and results.

MySample Data

x <- c("04-01-10","05-01-10","06-01-10","07-01-10","08-01-10","09-01-10","10-01-10","11-01-10","12-01-10","01-01-11","02-01-11","03-01-11","04-01-11","05-01-11","06-01-11","07-01-11","08-01-11","09-01-11","10-01-11","11-01-11","12-01-11","01-01-12","02-01-12","03-01-12","04-01-12","05-01-12","06-01-12")
y <- c(120,210,130,160,190,210,80,70,110,120,140,160,130,200,110,180,210,200,90,60,100,100,120,170,100,180,120)

x - date (symbol) in the format mm-dd-yy tz: IST (Calcutta / Asia) data has only one value y per month, which is located at the beginning of the month.

Convert to data frame

MySample <- data.frame(x) ## convert to dataframe 
MySample$y <- y

load required libraries

require(lubridate) 
require(ggplot2)

MySample Database

1) Graph x vs y

    ggplot(MySample, aes(MySample$x, MySample$y)) + 
        geom_bar(y=MySample$y,stat="identity") 

Give me the baseline plot results

2) Graph x vs y + fill = year

    ggplot(MySample, aes(MySample$x, MySample$y, fill=year(MySample$x))) + 
        geom_bar(y=MySample$y,stat="identity")

, 5 2010,2010,5,2011,2011,5,2012

, .

3) x vs y + fill = + facet_grid ()

    ggplot(MySample, aes(x, y, fill=year(x))) + 
        geom_bar(y=MySample$y,stat="identity") + 
        facet_grid(. ~ year(MySample$x)) 

Get: layout_base (, cols, drop = drop):      ,

4) x vs y + fill = year + facet_grid (year) + label-month (abbr)

    ggplot(MySample, aes(x, y, fill=year(x))) + 
        geom_bar(y=MySample$y,stat="identity") + 
        scale_x_date(labels=month(MySample$x,label=TRUE,abbr=TRUE))

: scale_labels.continuous(, ):

. :

  • 3 - 2010,2011,2012
  • xlabels -% b% y format; ; las2 ()
  • facet_grid
+5
1

3 :

  • use scale_fill_gradient (breaks = unique (MySample $year))
  • lubridate. ggplot2 date_format.
  • use: + facet_grid (. ~ , = "" )

:

MySample$date <- as.Date(MySample$x, "%m-%d-%y")
MySample$year <- year(MySample$date)

ggplot(MySample, aes(date, y, fill = year)) + 
  geom_bar(y=y,stat="identity") + 
  facet_grid(. ~ year, scales = "free") +
  scale_x_date(labels = date_format("%b/%y")) +
  scale_fill_gradient(breaks=unique(MySample$year))
+1

All Articles