library(ggplot2)ggplot2?A Slower Introduction to R
UCSF Library
Thursday, November 6, 2025





ggplot2 data visualization, specify 2 components:
ggplot2 data visualization, you must assign variables to dimensionslibrary()ggplot2 package:| action | analogy | frequency |
|---|---|---|
| installing | buying a cookbook | once ever |
| loading | taking the cookbook off the shelf | once per session |
library() code near the top of your scriptaes() function, inside the ggplot(nhanes,) function:aes() function stands for aestheticdata argument inside ggplot()data is the first argument so the argument name is optionalgeom_point()labs() function:color argument to aes()color argument to labs():geom_smooth() geometry'lm' stands for linear modelse=FALSE removes the default confidence bandAt the top of a new script, include code for loading the ggplot2 package and importing the data. Run your code.
In your script, write code for creating a scatter plot of systolic blood pressure (bpxosy1) versus age (ridageyr), with systolic blood pressure on the y axis. Add labels to the axes. Run your code.
smoking). Optionally, modify the label for the legend so that the first letter of smoking is capitalized. Run your code.geom_smooth() with the method='loess' argument. Also add a color argument to geom_smooth(), using some color of your choice (see this list). For example, you could use color='springgreen3'. Finally, add the se=FALSE argument to geom_smooth() to remove the default confidence band. Run your code.bpxosy1) versus hypertension (hypertension), with systolic blood pressure on the y axis. You can define the aesthetics component in a similar way as you did above. For the geometry component, use geom_boxplot() instead of geom_point(). Add labels to the axes using labs(). Run your code.bpxosy1) versus hypertension (hypertension), with systolic blood pressure on the y axis. You can replace geom_boxplot() above with geom_violin(). Add labels to the axes using labs(). Run your code.y argument in aes()geom_density() instead of geom_histogram():geom_histogram() is countsy argument to aes():size argument to geom_density():facet_grid() component:facet_grid() uses formula notationsmoking is listed first, meaning that it will define the rowsIn your script, write code for creating a density plot of diastolic blood pressure (bpxodi1), with color defining hypertension hypertension. Add labels to the axes. Optionally, change the size of the density line. Run your code.
Copy and paste the above code. Add faceting by gender (gender), with gender defining the columns. For the row dimension you can just type a period (.).

~/, which is your home directorygetwd():# define the data visualization
gg<-ggplot(nhanes,aes(x=bmxht,y=bpxosy1,color=gender))+
geom_point(color='gray')+
geom_smooth(method='lm',se=FALSE)+
labs(x='Height (cm)',
y='Systolic blood pressure (mmHg)',
color='Gender')
# output the data visualization
cairo_pdf('my figure.pdf',width=6,height=4)
print(gg)
dev.off()cairo_pdf() or pdf()png()jpeg() or tiff()print()dev.off()cairo_pdf and pdf(), the width and height are, by default, in inchespng(), the width and height are, by default in pixelsCreate a folder on your computer for this workshop series (or today’s session), if you haven’t already.
Set the working directory to that folder, using either method I mentioned.