##################################################################################### ## ## Intro STATS : Basic Graphics in R ## JDRS ## ##################################################################################### #Get the Crimean War Data crimea <- read.csv("http://reuningscherer.net/stat10x/data/crimean war.csv") #reformat data for this particular data levels=levels(crimea$Death_Cause) crimea2=cbind(crimea[crimea$Death_Cause==levels[1],3],crimea[crimea$Death_Cause==levels[2],3],crimea[crimea$Death_Cause==levels[3],3]) rownames(crimea2)=crimea[1:24,1] colnames(crimea2)=levels #see new data crimea2 ################################### #Bar Char for overall cause of death barplot(apply(crimea2,2,sum),names.arg=levels, main="Cause of Death", col='blue') #barchart by month and cause of death par(mar=c(8,3,2,1)) barplot(t(crimea2),xlab="", col=c("green", "red", "blue"), legend=colnames(crimea2), beside=TRUE, las=2) mtext("Month/Year", side=1, line=6) #Pie Chart pie(apply(crimea2,2,sum), labels=levels, main="Pie Chart of Death Cause") par(mar=c(5,4,4,2)+.1) ##################################### # Pie Chart with Percentages slices <- apply(crimea2,2,sum) pct <- round(slices/sum(slices)*100) lbls <- paste(levels, pct) # add percents to labels lbls <- paste(lbls,"%",sep="") # ad % to labels pie(slices,labels = lbls, col=rainbow(length(lbls)), main="Pie Chart of Death Cause") #3D Pie Chart library(plotrix) pie3D(slices,labels=lbls,explode=0.05, main="Pie Chart of Death Cause ") #for a polar graph, see https://rdrr.io/cran/HistData/man/Nightingale.html ##################################### # Histogram for World Bank Fertility Data #Get WB Data WB <- read.csv("http://reuningscherer.net/stat10x/data/WorldBank2013.csv") #see variables available names(WB) #Make a histrogram of fertility rates hist(WB$Fertility, col="blue", main="Histogram of Fertility Rates", xlab="Avg Births Per Woman Over Lifetime", breaks=10) ##################################### # DotPlots for World Bank Data # Called a STRIPCHART in R #Life Expectancy stripchart(round(WB$LifeExp), method="stack", offset=.5, at=.15, pch=19, cex=1.3, col="blue", main="Dot Plot of Fertility Rates", xlab="Years") #GNI per capita stripchart(round(WB$GNI/1000), method="stack", offset=.5, at=.15, pch=19, cex=1.3, col="blue", main="Dot Plot of GNI Per Capita Rates", xlab="GNI (Thousands of Dollar Equivalents)") #histogram for comparison hist(WB$GNI/1000, col="blue", main="Histogram of GNI per Capita", xlab="GNI per Capita (Thousands of Dollars)", breaks=10) ######################################## # Boxplots #Boxplot of Fertility Rates boxplot(WB$Fertility, col="yellow", lwd=2, main="Boxplot of Fertility Rates", ylab="Avg Number of Children per Woman") #Horizontal Boxplot boxplot(WB$Fertility, col="yellow", lwd=2, main="Boxplot of Fertility Rates", xlab="Avg Number of Children per Woman", horizontal=T) #Boxplot for Roundup Data #get data roundup <- read.csv("http://reuningscherer.net/stat10x/data/roundup.csv") #just radishes radish=roundup[1:15,] #Boxplot for radish by concentration boxplot(radish$Dry.Weight~radish$Concentration, col="green", lwd=2, main="Radish Dry Weight by Concentration", ylab="Dry Weight", xlab="Concentration")