R For SAS Users
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University

ggplot2 is a powerful graphics package for Rggplot stands for the "grammar of graphics"ggplot2 uses a layering approach to build graphics# Create plot for x=sex and y=diameter
ggplot(data = abalone, aes(sex, diameter))
ggplot()data = abaloneSet aes to sex and diameter
No graphical objects in the plot yet
sexdiameter
# Add boxplot geometric object or geom
ggplot(data = abalone,
aes(sex, diameter)) +
geom_boxplot()
+ adds layerBoxplot geom_boxplot() added
Result is series of boxplots
F females, I infants, and M males
# Add black white theme
ggplot(data = abalone,
aes(sex, diameter)) +
geom_boxplot() +
theme_bw()
theme_bw()
# Change to geom_violin()
ggplot(data = abalone,
aes(sex, diameter)) +
geom_violin() +
theme_bw()
geom_violin replaces geom_boxplot
# Make histogram of shuckedWeight
ggplot(abalone, aes(shuckedWeight)) +
geom_histogram()
geom_histogram()aes() to shuckedWeight
# Make lines black and fill light blue
ggplot(abalone, aes(shuckedWeight)) +
geom_histogram(color = "black",
fill = "lightblue")
color of bin linesfill color for bins()
# Add x, y axis labels and title
ggplot(abalone, aes(shuckedWeight)) +
geom_histogram(color = "black",
fill = "lightblue") +
xlab("Shucked Weight") +
ylab("Frequency Counts") +
ggtitle("Shucked Weights Histogram")
xlab() and ylab() for axesggtitle() for title
# Make scatterplot with geom_point()
ggplot(abalone,
aes(rings, shellWeight)) +
geom_point()
aes needs two variablesgeom_point() adds the points
# Add smoothed fit line
ggplot(abalone,
aes(rings, shellWeight)) +
geom_point() +
geom_smooth()
geom_smooth() line
# Add panels using facet_wrap()
ggplot(abalone,
aes(rings, shellWeight)) +
geom_point() +
geom_smooth() +
facet_wrap(vars(sex))
facet_wrap() layervars(sex) defines variable for panels
ggplot2 graphical skills foundationR For SAS Users