Plotting in R with graphics
Introduction
The graphics
package is included with the language, meaning you won’t need to import anything at the beginning of your file. It includes a ton of useful, variably-complex plots to use on your journey of data visualization.
Examples
Plot of the table of Age values for the subset of the DeathRecords data file for which Sex=='F'.
Click to see solution
deathDF <- read.csv("/anvil/projects/tdm/data/death_records/DeathRecords.csv")
femaleSubset <- subset(deathDF, Sex == 'F')
ageTable <- table(femaleSubset$Age)
plot(ageTable, main = "Age Distribution of Females", xlab = "Age", ylab = "Frequency")
Plot of the table of Age values for the subset of the DeathRecords data file for which Sex=='F' & Age!=999.
Click to see solution
validFemaleSubset <- subset(deathDF, Sex == 'F' & Age != 999)
validAgeTable <- table(validFemaleSubset$Age)
plot(validAgeTable, main = "Age Distribution of Females (w/o 999)", xlab = "Age", ylab = "Frequency")
Plot of the table of Age values for people with Filipino race, also with Age not equal to 999.
You can see what races the data from the Race column represents, by looking at page 15 of the pdf source file: www.cdc.gov/nchs/data/dvs/Record_Layout_2014.pdf
Click to see solution
plot(table(deathDF$Age[(deathDF$Race == 7) & (deathDF$Age != 999)]))
Wrap the table into a barplot that shows the number of people in each of the 6 categories below.
"youth": less than or equal to 18 years old "young adult": older than 18 but less than or equal to 25 years old "adult": older than 25 but less than or equal to 35 years old "middle age adult": older than 35 but less than or equal to 55 years old "senior adult": greater than 55 years old but less than or equal to 150 years old (or any other upper threshold that you like) "unknown": age of 999 (you could use, say, ages 150 to Inf for this category) |
Click to see solution
death_records <- read.csv("/anvil/projects/tdm/data/death_records/DeathRecords.csv")
ages <- death_records$Age
second_age_groups <- cut(ages,
breaks = c(0, 18, 25, 35, 55, 150, Inf),
labels = c("youth", "young adult", "adult", "middle age adult", "senior adult", "unknown"))
second_table <- table(second_age_groups)
barplot(second_table, main = "Age Group Deaths", xlab = "Age Group", ylab = "Number of People", cex.names = 0.64)