使用ggplot绘图系统绘制图形

时间:2015-07-25 16:18:43

标签: r graph

我有以下数据框

   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

我想使用Measure系统为每个Year分别绘制type Vs ggplot2图。有人可以帮助我获得情节。我想为每个Measure

制作一张包含Year Vs type子图的单个地块

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
        = "h.wickham@gmail.com"), person("Winston", "Chang", role =
        "aut", email = "winston@stdout.org") )
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 <h.wickham@gmail.com>
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 

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

1 个答案:

答案 0 :(得分:1)

这样的东西?

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