Scatterplot and line graphs
Good habits
Less good habits
.csv
files and scriptsNote that one file ends in .Rmd
. You may be familiar, you may not. Today we will also introduce .qmd
.
Content | Link |
---|---|
Metrics of “adulthood” | Pew Research Center |
“Proportional ink” | Bergstron and West |
I have digitized and provided the data. Using base R for now, we will,
Resist the urge to copy and paste code (at least the first few times you encounter a new command).
Typos are a valuable part of the learning process.
Unzip the template to start, then
.RProj
file from your file browser..Rmd
file.
Please remember that among you are both advanced programmers and nervously excited newcomers.
Next to each #|
is a code block option. If you set eval:
to true
, your calculation will return a value.
read.delim()
(this is one of many largely interchangable functions).Graphing syntax is flexible.
Looking at our graph, are there other ways to visualize this data?
It is accepted (and expected) that bar graphs should start at zero.
The bar for 1983 is
taller than the bar for 2023.
Line graphs are evaluated
“Instead, a line chart should be scaled so as to make the position of each point maximally informative, usually by allowing the axis to span the region not much larger than the range of the data values.” - Bergstrom and West
We can add optional, comma-separated arguments to refine the appearance.
type = 'l'
to set a l
ine typexlim = c(1980, 2025)
to set horixontal axis limitsylim = c(0, 100)
to set vertical axis limitslas = 1
to rotate vertical axis tick labels?plot
for moreTry (feel free to omit the line break).
These are largely interchangeable, but each has unique benefits.
x
” and “y
” simultaneouslyx, y
” separately (contrast with “y ~ x
”)We might want to modify axes labels in a number of ways. As it is, they are drawn directly from our variable names.
We could,
xlab = "Time (Years)"
)font = 2, size = 1.5
)xlab = ""
) and add labels with the more flexible mtext()
function (next)A flexible command par()
accepts a variety of layout and aesthetic specifications.
Include the following line before the plot()
command you have been using.
By going back and forth between graphs (or using ?par
) learn about its arguments.
Change par(mfrow = c(1, 1), ... )
to par(mfrow = c(2, 2), ... )
and in the three new panels add separate plots for the remaining milestones of adulthood described in the data.
Experiment with
main = ...
in the original plot or mtext(..., side = 3, ...)
after the plot)By its appearance, we made a “connected line plot”.
lty
from l
to b
to p
.Set par(mfrow = c(1, 1))
and plot the percent living independently against time.
By using lines()
we can add lines corresponding to the values for the remaining three milestones.
This can be done simply by copy-paste-edit, or a bit more elegantly with a loop.
Contemplate the apperance of your graph - should it have a legend or line labels?