This report is automatically generated with the R
package knitr
(version 1.5
)
.
# Chapter 18 - Looking At ggplot2 Graphics Installing and Loading ggplot2 install.packages("ggplot2")
## Warning in install.packages : ## package 'ggplot2' is in use and will not be installed
library("ggplot2") # Looking At Layers ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_point() + stat_smooth()
# Using Geoms and Stats Defining what data to use Mapping data to plot aesthetics ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_point() + stat_smooth()
## Getting geoms Creating a bar chart ggplot(quakes, aes(x = depth)) + geom_bar()
ggplot(quakes, aes(x = depth)) + geom_bar(binwidth = 50)
quakes.agg <- aggregate(mag ~ round(depth, -1), data = quakes, FUN = length) names(quakes.agg) <- c("depth", "mag") ggplot(quakes.agg, aes(x = depth, y = mag)) + geom_bar(stat = "identity")
### Making a scatterplot ggplot(quakes, aes(x = long, y = lat)) + geom_point()
### Creating line charts ggplot(longley, aes(x = Year, y = Unemployed)) + geom_line()
# Sussing Stats Binning data ggplot(quakes, aes(x = depth)) + geom_bar(binwidth = 50)
ggplot(quakes, aes(x = depth)) + stat_bin(binwidth = 50)
## Smoothing data ggplot(longley, aes(x = Year, y = Employed)) + geom_point()
ggplot(longley, aes(x = Year, y = Employed)) + geom_point() + stat_smooth()
ggplot(longley, aes(x = Year, y = Employed)) + geom_point() + stat_smooth(method = "lm")
# Adding Facets, Scales, and Options Adding facets ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point()
ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + stat_smooth(method = "lm") + facet_grid(~cyl)
ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(shape = factor(cyl), colour = factor(cyl)))
ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(shape = factor(cyl), colour = factor(cyl))) + scale_shape_discrete(name = "Cylinders") + scale_colour_discrete(name = "Cylinders")
## Changing options ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(color = "red") + xlab("Performance (horse power") + ylab("Fuel consumption (mpg)") + opts(title = "Motor car comparison")
The R session information (including the OS info, R version and all packages used):
sessionInfo()
## R version 3.0.2 (2013-09-25) ## Platform: x86_64-w64-mingw32/x64 (64-bit) ## ## locale: ## [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 ## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C ## [5] LC_TIME=English_United Kingdom.1252 ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## other attached packages: ## [1] BiocInstaller_1.12.1 ggplot2_0.9.3.1 reshape2_1.2.2 sos_1.3-8 ## [5] brew_1.0-6 stringr_0.6.2 knitr_1.5 plyr_1.8 ## [9] Revobase_7.1.0 RevoMods_7.1.0 RevoScaleR_7.1.0 lattice_0.20-27 ## [13] rpart_4.1-2 ## ## loaded via a namespace (and not attached): ## [1] codetools_0.2-8 colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4 ## [5] evaluate_0.5.1 foreach_1.4.1 formatR_0.10 fortunes_1.5-2 ## [9] grid_3.0.2 gtable_0.1.2 highr_0.3 iterators_1.0.6 ## [13] labeling_0.2 MASS_7.3-29 munsell_0.4.2 proto_0.3-10 ## [17] RColorBrewer_1.0-5 scales_0.2.3 tools_3.0.2
Sys.time()
## [1] "2014-05-13 15:06:45 BST"