Statistical Computing, 36-350
Wednesday July 10, 2019
apply
subset()
: function for extracting rows of a data frame meeting a conditionsplit()
: function for splitting up rows of a data frame, according to a factor variableapply()
: function for applying a given routine to rows or columns of a matrix or data framelapply()
: similar, but used for applying a routine to elements of a vector or listsapply()
: similar, but will try to simplify the return type, in comparison to lapply()
tapply()
: function for applying a given routine to groups of elements in a vector or list, according to a factor variablePlot basics: ggplot
There’s 2 major styles of plotting in R
. You’ve already seen examples of base R
plots include
plot()
: generic plotting functionhist()
: histogramBut… the R
community has come to embrace a new approach to visualization from the ggplot2
package.
Extremely popular graphics library
ggplot
: The main function where you specify the dataset and variables to plotgeoms
: geometric objects
geom_point()
, geom_bar()
, geom_density()
, geom_line()
, geom_area()
aes
: aesthetics
shape
, transparency (alpha
), color
, fill
, linetype
.scales
: Define how your data will be plotted
library(gapminder)
data(gapminder)
library(ggplot2)
ggplot(gapminder, aes(y = lifeExp, x = gdpPercap)) +
geom_point()
ggplot(data = gapminder, aes(y = lifeExp, x = gdpPercap)) +
geom_point()
p <- ggplot(data = gapminder, aes(y = lifeExp, x = gdpPercap))
p + geom_point()
ggplot
function (in this case data
and variable mappings)ggplot(gapminder, aes(y = lifeExp, x = gdpPercap)) +
geom_point(size = .1)
ggplot(gapminder, aes(y = lifeExp, x = gdpPercap, color = continent)) +
geom_point(size = .1)