6 Visualization
ggplot2::
offers a range of convenient functions for data visualization. The foundational function, ggplot()
, provides the initial framework for adding supplementary layers using the +
operator. In ggplot(), we define variables plotted on x- and y-axis through aes()
. For example:
# without pipe
ggplot(data = iris,
mapping = aes(x = Sepal.Length,
y = Sepal.Width)) +
# additional layers...
# with pipe
iris %>%
ggplot(mapping = aes(x = Sepal.Length,
y = Sepal.Width)) +
# additional layers...
Please note that aes()
refers to columns in the data frame. Variables names that do not exist in the data frame cannot be used.
6.0.1 Point
geom_point()
: Add a point layer
# basic plot
iris %>%
ggplot(aes(x = Sepal.Length,
y = Sepal.Width)) +
geom_point()
# change color by "Species" column
iris %>%
ggplot(aes(x = Sepal.Length,
y = Sepal.Width,
color = Species)) +
geom_point()
6.0.2 Line
geom_line()
: Add a line layer
6.0.3 Histogram
geom_histogram()
: add a histogram layer
# basic plot; bins = 30 by default
iris %>%
ggplot(aes(x = Sepal.Length)) +
geom_histogram()
# change bin width
iris %>%
ggplot(aes(x = Sepal.Length)) +
geom_histogram(binwidth = 0.5)
# change bin number
iris %>%
ggplot(aes(x = Sepal.Length)) +
geom_histogram(bins = 50)
6.0.4 Boxplot
geom_boxplot()
: add a boxplot layer
# basic plot
iris %>%
ggplot(aes(x = Species,
y = Sepal.Length)) +
geom_boxplot()
# change fill by "Species"
iris %>%
ggplot(aes(x = Species,
y = Sepal.Length,
fill = Species)) +
geom_boxplot()
# change fill by "Species", but consistent color
iris %>%
ggplot(aes(x = Species,
y = Sepal.Length,
fill = Species)) +
geom_boxplot(color = "darkgrey")