0

I have the following data frame

   year   type  Measure
1  1989    NP   2107
2  2002    NP   109
3  2003    NP   159
4  2008    NP   137
5  1989    NR   522
6  2002    NR   240
7  2003    NR   248
8  2008    NR   55
9  1989    OR   346
10 2002    OR   134
11 2003    OR   130
12 2008    OR   88
13 1989    P    296
14 2002    P    569
15 2003    P    1202
16 2008    P    34

I want to plot Measure Vs Year plot separately for each type using the ggplot2 system. Can someone help me in getting the plot. I want a single plot with Measure Vs Year subplots for each type

The output of packageDescription("ggplot2") :

packageDescription("ggplot2")
Package: ggplot2
Type: Package
Title: An Implementation of the Grammar of Graphics
Version: 1.0.1
Authors@R: c( person("Hadley", "Wickham", role = c("aut", "cre"), email
        = "[email protected]"), person("Winston", "Chang", role =
        "aut", email = "[email protected]") )
Description: An implementation of the grammar of graphics in R. It
        combines the advantages of both base and lattice graphics:
        conditioning and shared axes are handled automatically, and you
        can still build up a plot step by step from multiple data
        sources. It also implements a sophisticated multidimensional
        conditioning system and a consistent interface to map data to
        aesthetic attributes. See http://ggplot2.org for more
        information, documentation and examples.
Depends: R (>= 2.14), stats, methods
Imports: plyr (>= 1.7.1), digest, grid, gtable (>= 0.1.1), reshape2,
        scales (>= 0.2.3), proto, MASS
Suggests: quantreg, Hmisc, mapproj, maps, hexbin, maptools, multcomp,
        nlme, testthat, knitr, mgcv
VignetteBuilder: knitr
Enhances: sp
License: GPL-2
URL: http://ggplot2.org, https://github.com/hadley/ggplot2
BugReports: https://github.com/hadley/ggplot2/issues
LazyData: true
Collate: 'aaa-.r' 'aaa-constants.r' 'aes-calculated.r' .....
Packaged: 2015-03-16 20:29:42 UTC; winston
Author: Hadley Wickham [aut, cre], Winston Chang [aut]
Maintainer: Hadley Wickham <[email protected]>
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2015-03-17 17:49:38
Built: R 3.2.1; ; 2015-07-19 04:13:46 UTC; unix

-- File: /home/R/i686-pc-linux-gnu-library/3.2/ggplot2/Meta/package.rds 

output of dput(head(main_data))

dput(head(main_data))
structure(list(Measure = c(6.532, 
78.88, 0.92, 10.376, 10.859, 83.025), type = c("P", "P", 
"P", "P", "P", "P"), year = c(1989L, 1989L, 1989L, 
1989L, 1989L, 1989L)), .Names = c("Measure", "type", "year"), row.names = c("114288", "114296", 
"114300", "114308", "114325", "114329"), class = "data.frame")
0

1 Answer 1

1

Something like this?

df <- structure(list(year = c(1989L, 2002L, 2003L, 2008L, 1989L, 2002L, 
    2003L, 2008L, 1989L, 2002L, 2003L, 2008L, 1989L, 2002L, 2003L, 
    2008L), type = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("    NP  ", "    NR  ", 
    "    OR  ", "    P   "), class = "factor"), Measure = c(2107L, 
    109L, 159L, 137L, 522L, 240L, 248L, 55L, 346L, 134L, 130L, 88L, 
    296L, 569L, 1202L, 34L)), .Names = c("year", "type", "Measure"
    ), class = "data.frame", row.names = c(NA, -16L))


ggplot(df, aes(x=year, y=Measure)) + 
    geom_bar(stat='identity') + 
    facet_grid(. ~ type)

enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

When I try there is no plot as output. It is simply exits from the code. Why is that happening to me?
plotdata <- aggregate(Measure ~ year + type, main_Data, FUN=sum). When I did this I got plot data to be the data frame that I posted in the question then as you mentioned I did ggplot(plotdata, aes(x=year, y=Measure)) + geom_bar(stat='identity') + facet_grid(. ~ type). But there is no plot as output. why is this happening
Could you post your data as a dput output and a result of packageDescription("ggplot2")?
The data has thousands of rows and it cant fit in one go. I have posted the output of packageDescription
I didn't mean all :) dput(head(df)) is usually enough.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.