About the ggplot2 Package
Author(s) -
Hadley Wickham
Publication year - 2016
Publication title -
journal of applied and computational mathematics
Language(s) - English
Resource type - Journals
ISSN - 2168-9679
DOI - 10.4172/2168-9679.1000321
Subject(s) - computer science
"ggplot2 is an R package for producing statistical, or data, graphics, but it is unlike most other graphics packages because it has a deep underlying grammar. This grammar, based on the Grammar of Graphics, is composed of a set of independent components that can be composed in many different ways [1-3]. Plots can be built up iteratively and edited later. A carefully chosen set of defaults means that most of the time you can produce a publication-quality graphic in seconds, but if you do have special formatting requirements, a comprehensive theming system makes it easy to do what you want [4]. ggplot2 is designed to work in a layered fashion, starting with a layer showing the raw data then adding layers of annotation and statistical summaries [5,6]." "ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics." Tutorial Sample code for the illustration of ggplot2 install & load ggplot library install.package("ggplot2") library("ggplot2") show info about the data head(diamonds) head(mtcars) comparison qplot vs ggplot # qplot histogram qplot(clarity, data=diamonds, fill=cut, geom="bar") # ggplot histogram -> same output ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() (Figure 1) How to use qplot # scatterplot qplot(wt, mpg, data=mtcars) # Transform input data with functions qplot(log(wt), mpg 10, data=mtcars) # add aesthetic mapping (hint: how does mapping work) qplot(wt, mpg, data=mtcars, color=qsec) (Figure 2) # change size of points (hint: color/colour, hint: set aesthetic/mapping) qplot(wt, mpg, data=mtcars, color=qsec, size=3) qplot(wt, mpg, data=mtcars, colour=qsec, size=I(3)) {aesthetics can be set to a constant value instead of mapping} # use alpha blending qplot(wt, mpg, data=mtcars, alpha=qsec) {values between 0 (transparent) and 1 (opaque)} # continuous scale vs. discrete scale head(mtcars) qplot(wt, mpg, data=mtcars, colour=cyl) levels(mtcars$cyl) qplot(wt, mpg, data=mtcars, colour=factor(cyl)) (Figure 3)
Accelerating Research
Robert Robinson Avenue,
Oxford Science Park, Oxford
OX4 4GP, United Kingdom
Address
John Eccles HouseRobert Robinson Avenue,
Oxford Science Park, Oxford
OX4 4GP, United Kingdom