Visualization
in R
GGPLOT
1. Install library
install.packages("tidyverse")
2. library(tidyverse)
Section 1 Title
ggplot2 is a plotting package that provides helpful
commands to create complex plots from data in a data
frame. It provides a more programmatic interface for
specifying what variables to plot, how they are displayed,
and general visual properties. Therefore, we only need
minimal changes if the underlying data change or if we
decide to change from a bar plot to a scatterplot. This helps
in creating publication quality plots with minimal amounts of
adjustments and tweaking.
Section 1 Title
ggplot2 refers to the name of the package itself. When using
the package we use the function ggplot() to generate the plots,
and so references to using the function will be referred to
as ggplot() and the package as a whole as ggplot2
Plots work best with data in the ‘long’ format, i.e., a column for
every variable, and a row for every observation. Well-structured
data will save you lots of time when making figures with ggplot2
ggplot graphics are built layer by layer by adding new elements.
Adding layers in this fashion allows for extensive flexibility and
customization of plots.
Section 1 Title
ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) +
<GEOM_FUNCTION>()
1. ggplot(data = surveys_complete)
Section 1 Title
ggplot(data = surveys_complete, mapping =
aes(x = weight, y = hindfoot_length))
✓ define an aesthetic mapping (using the aesthetic (aes)
function), by selecting the variables to be plotted and specifying
how to present them in the graph, e.g., as x/y positions or
characteristics such as size, shape, color, etc
Section 1 Title
3. ggplot(data = surveys_complete, aes(x =
weight, y = hindfoot_length)) + geom_point()
•add ‘geoms’ – graphical representations of the data in the plot
(points, lines, bars). offers many different geoms; we will use
ggplot2
some common ones today, including:
•Geom_point()-for scatter plots, dot plots, etc.
•Geom_boxplot()- for, well, boxplots!
•Geom_line()-for trend lines, time series, etc.
o add a geom to the plot use + operator. Because we have two
continuous variables
Section 1 Title
The + in the ggplot2 package is particularly useful because it
allows you to modify existing ggplot objects.
This means you can easily set up plot “templates” and
conveniently explore different types of plots, so the above plot can
also be generated with code like this :
# Assign plot to a variable
surveys_plot <- ggplot(data = surveys_complete,
mapping = aes(x = weight, y = hindfoot_length))
# Draw the plot
surveys_plot + geom_point()
Section 1 Title
• You may notice that we sometimes reference ‘ggplot2’ and
sometimes ‘ggplot’. To clarify, ‘ggplot2’ is the name of the most
recent version of the package. However, any time we call the
function itself, it’s just called ‘ggplot’.
•The previous version of the ggplot2 package, called ggplot,
which also contained the ggplot() function is now unsupported
and has been removed from CRAN in order to reduce accidental
installations and further confusion.
Program 1
library(tidyverse)
BOD
ggplot(data=BOD,
mapping=aes(x=Time,
y=demand))+
geom_point(size=5)
Program 2
library(tidyverse)
fd1 <- data.frame(
Q=c(5,7,12,13,25),
s=c(120,150,45,35,44))
ggplot(data = fd1, mapping = aes(x = Q,
y = s))+
geom_point(size=4,color="green")+
geom_line()