GGPLOT2 Package
It is a graphic package implemented in R, it allows to make graphs in a very flexible way since it contains many different commands to perform specific functions when creating and / or manipulating graphs.
In addition, this graphic package has an underlying grammar in layers, so that it is composed of components. These independent components can be combined in different ways to make or modify the chart. Each graph is made from a few equal components: data, visual marks representing data points (geoms), and a coordinate system. The necessary variables for the ggplot command are the data to be used in data frame format and know which variables we will use as x and y:
ggplot (df, aes (x, y, ))
You can add more layers to the previous ggplot using the
+
operator, so the code is explicit as to which layers were added and in the order they were added.
Examples:
require(ggplot2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.1
ggplot(ChickWeight, aes (Time, weight)) +
geom_point(aes (color = Diet)) +
geom_smooth(aes(x=Time, y=weight, color = Diet), method = 'lm')
ggplot(ChickWeight, aes (Time, weight, colour = Diet)) +
geom_point() +
facet_grid(. ~ Diet)
LATTICE Package
It is a powerful and elegant system of data visualization of high level, especially useful for the manipulation of data with multiple variables. Specifically, the package allows the creation of Trellis graphs: graphs that show a variable or the relationship between variables, providing one or more variables.
It is designed to simplify most of the typical graphs, but it can also be adapted to perform less usual graphic requirements.
The basic call is:
Graph_type(formula, data =)
In general the formula for specifying a graph is y ~ x | F,
Y = vertical axis
X = horizontal axis
F = clustering factor or expression
To know the graph types in lattice:
?lattice
In some cases you can omit the variable
y
and work with a single variable. The clustering expression may be formed by different factors separated by an arithmetic operator, usually indicates the crossing of factors.
For example : hist (~ x | f1 * f2, data = data)
Examples:
require(lattice)
## Loading required package: lattice
histogram(~weight|Diet, data=ChickWeight)
xyplot(weight~Time|Diet, data= ChickWeight)
xyplot(weight~Time|Chick*Diet, data= ChickWeight)
densityplot(~weight|Diet, data=ChickWeight)