R graphics : ggplot2
Introduction
library(ggplot2)
A library created and developped by Hadley Wickman, that improves the base and lattice systems by using a more formal underlying model. It is based on the concept of grammar of graphics, which describes the components that underline statistics graphics.
Components:
- Data we want to visualize, and aestetic mappings between this data and attribute we can perceive.
- Geoms (geometric objects) what we see on the plot (point line…).
- Stats (statistical transformation) summarize, count observations, transformation.
- Scales: legends, axes, etc. which make it possible to read the data from the graph.
- Coord: a coordinate system.
- Faceting: how to break up data into subsets.
The official website with code examples and documentation is http://ggplot2.org
See also : Ggplot2 “How to”
Installation
install.packages("ggplot2")
Development version of the source code is also on GitHub.
Qplot() function
The equivalent of the general purpose plot() base function.
When graphs are splitted by “facets”, the group order depends on the corresponding factor's order (alphabetical, by default).
Tests on the Iris data set.
data(iris)
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.30 Min. :2.00 Min. :1.00 Min. :0.1
## 1st Qu.:5.10 1st Qu.:2.80 1st Qu.:1.60 1st Qu.:0.3
## Median :5.80 Median :3.00 Median :4.35 Median :1.3
## Mean :5.84 Mean :3.06 Mean :3.76 Mean :1.2
## 3rd Qu.:6.40 3rd Qu.:3.30 3rd Qu.:5.10 3rd Qu.:1.8
## Max. :7.90 Max. :4.40 Max. :6.90 Max. :2.5
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
# Default
qplot(x = Sepal.Length, y = Petal.Length, data = iris)
# One graph with points
qplot(x = Sepal.Length, y = Petal.Length, data = iris, geom = "point")
# One graph with points, colors function of Species
qplot(x = Sepal.Length, y = Petal.Length, data = iris, geom = "point", colour = Species)
# Multiple point graphs splitted by Species
qplot(x = Sepal.Length, y = Petal.Length, data = iris, geom = "point", facets = Species ~
., colour = Species)
# Densities splitted by Species
qplot(x = Sepal.Length, data = iris, geom = "density", colour = Species)
# Other types of graphs : combining geoms
qplot(x = Sepal.Length, y = Petal.Length, data = iris, geom = c("smooth", "point"),
facets = Species ~ ., colour = Species)
qplot(x = Species, y = Petal.Length, data = iris, geom = c("boxplot", "jitter"),
colour = Species)

This work by Celine Hernandez is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.