############################################### ## Author: Joshua M. Tebbs ## Date: 27 July 2016 ## Update: 15 March 2024 ## STAT 110 course notes: R Code Chapter 10 ############################################### # Figure 10.1 # Page 78 # Find data web site # Cut and paste data set into a notepad file # Save this file (e.g., nutrition.txt) to a folder on your laptop or desktop # Import the data from the folder # This creates a table (with each row corresponding to one infant) nutrition.type = read.table("C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\nutrition.txt",header=TRUE) # This creates a univariate data set (ignoring infant case number) nutrition.type = nutrition.type[,1] # This creates a table of the data # There are 605 data values for the nutrition type variable (10 values were missing) count = table(nutrition.type) count # Create table but use proportions instead of counts proportion = count/605 round(proportion,2) # Create labels for the categories of nutrition type labels = c("Breastmilk","Fluids","TPN","Formula") # Barplot of counts (left) barplot(count, main="",xlab="Nutrition Type",ylab="Count",names.arg=labels,ylim=c(0,300),col="lightblue") # Barplot of proportions (right) barplot(proportion,main="",xlab="Nutrition Type",ylab="Proportion",names.arg=labels,ylim=c(0,0.5),col="lightblue") # Figure 10.2 # Page 79 # Create pie chart of proportions proportion = c(0.39,0.10,0.44,0.07) labels = c("Breastmilk","Fluids","TPN","Formula") pie(proportion,labels = proportion,main = "",col=c("lightblue","khaki","lightcoral","lightgreen")) legend("topright",labels,cex = 1,fill=c("lightblue","khaki","lightcoral","lightgreen")) # Figure 10.3 # Page 80 # Construct bar graph of states (percentage of residents with a bachelor's degree) percents = c(27.4,25.3,33.2,34.6,26.5,34.9,24.8,31.5,30.5,41.8) labels = c("AL","AR","FL","GA","LA","NC","MS","SC","TN","VA") # Figure 10.3 (left) barplot(percents,main="Percentage of Residents with Bachelor's Degree",xlab="State",ylab="Percent",ylim = c(0,50.0),names.arg=labels,col="lightblue") # Plot in descending order # Figure 10.3 (right) labels.sort = c("VA","NC","GA","FL","SC","TN","AL","LA","AR","MS") barplot(sort(percents,decreasing=TRUE),main="Percentage of Residents with Bachelor's Degree",xlab="State",ylab="Percent",ylim = c(0,50.0),names.arg=labels.sort,col="lightblue") # Figure 10.4 # Page 82 enrollment = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\enrollment.txt",header=TRUE),start=1954) plot(enrollment,ylab="Number of students",xlab="Year",type="o",ylim=c(0,40000),pch=19,cex=0.75) # Figure 10.5 # Page 84 # Email me if you would like either data set # Airmiles data (top) data(airmiles) # TSA package; need to install (see me) plot(airmiles/1000,ylab="Number of airline miles (in 1000s)",xlab="Year",type="o",pch=19,cex=0.75) # Home run data (bottom) homeruns = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat520\\f13\\data\\homeruns.txt",header=TRUE),start=1909) plot(homeruns,ylab="Number of home runs",xlab="Year",type="o",pch=19,cex=0.75) # Figure 10.6 # Page 85 # Email me if you would like either data set # Earthquake data (top) earthquake = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat520\\f13\\data\\earthquake.txt",header=TRUE),start=1900) plot(earthquake,ylab="Number of earthquakes",xlab="Year",type="o",pch=19,cex=0.75) # Supreme Court data (bottom) homeruns = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat520\\f13\\data\\homeruns.txt",header=TRUE),start=1909) plot(homeruns,ylab="Number of home runs",xlab="Year",type="o",pch=19,cex=0.75) # Figure 10.7 # Page 86 # Yearly case count (left) yearly.tb.cases = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\yearly.tb.cases.txt",header=TRUE),start=1953) plot(yearly.tb.cases,ylab="Number of TB cases",xlab="Year",type="o",pch=19,cex=0.75)) # Monthly case count (right) monthly.tb.cases = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\monthly.tb.cases.txt",header=TRUE),start=2000,freq=12) plot(monthly.tb.cases,ylab="Number of TB cases",xlab="Year",type="o",pch=19,cex=0.75) # Figure 10.8 # Page 89 # Create labels for the categories of nutrition type labels = c("Trump","Reagan","Bush","Clinton","Bush Jr","Obama") count = c(24,19,18,18,16,15) # Create barplot of counts barplot(count,main="",xlab="President",ylab="Number of appellate judgeships confirmed",names.arg=labels,ylim=c(0,25),col="lightblue") # Figure 10.9 # Page 90 temperature = ts(read.table(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\temperature.txt",header=TRUE),start=1875) # Compressed vertical axis (top) plot(temperature,ylab="Average global surface temperature (deg F)",xlab="Year",type="o",pch=19,cex=0.75,ylim=c(35,70)) # Better vertical axis (bottom) plot(temperature,ylab="Average global surface temperature (deg F)",xlab="Year",type="o",pch=19,cex=0.75,ylim=c(48,57))