Answers – Hwk2B
Just some code and notes, needs editing and add the graphs.
Answers to odd numbered questions for Homework 2B: Graphs
# 1.
strains <- c("DBA","DBA","DBA","DBA","BL6","BL6","BL6","BL6","FVB","FVB","FVB","FVB","129","129","129","129")
x <- c(30.5,48.8, 37.4,56.6,120.4,149,122.7,104.4,182.8,186.7,110.1,102.8,135.8,129.7,131.1,90.3)
myData <- data.frame(strains,x)
attach(myData)
# Rcmdr uses the function Boxplot from the car package.
# method="y" relates to id argument and how outlier points are labeled. Try method="identify" for interactive.
car::Boxplot(x ~ strains, data=myData, id=list(method="y"))
# code for boxplot base R graphics.
boxplot(x ~ strains, data=myData)
# By default, R only lets you save images as pdf files. You want png or jpg to work best with your reports.
# Perfectly OK to just take screenshots (and rename the file, of course).
# Or, want to save the graph to a file in your working directory?
# Here are the steps.
getwd() #first, confirm your working folder -- this is where R will save your graph.
# I suggest png format - this next line initiates the plotting device.
png("Hwk02B_q1.png", width = 400, height = 300, units = "px", res = 96)
boxplot(y ~ x.mice, data=x.df, id=list(method="y"))
# Last, we have to close the plotting device.
dev.off()
</pre>
# 3.
<pre>
d.month = c(“Jan”,”Jan”, “Jan”,”Mar”,”Mar”,”Mar”)
sulfateppm =c(14.3, 14.8, 14.8,9.3,9.4,9.3)
try = data.frame(d.month,sulfateppm)
attach(try)
# Get the means
byWater = tapply(sulfateppm,list(Group=d.month),mean); byWater
# Next, we try a different boxplot function.
install.packages(“gplots”)
library(gplots)
byWater
boxplot2(sulfateppm ~ d.month, ylim=c(0,20), col=”blue”)
boxplot(sulfateppm ~ d.month, ylim=c(0,20), col=”blue”)
boxplot2(sulfateppm~d.month, ylim=c(0,20), col=”blue”, ylab=”Sulfates, ppm”)
box(col = “red”, lwd = 2)
</pre>
#5.
<pre>
years <-c (1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020)
co2 <- c(316.19, 319.42, 325.13, 330.62, 338.29, 346.12, 354.41, 360.82, 396.83, 380.31, 389.99, 402.06, 414.26)
my.data <- data.frame(years,co2)
plot(my.data)
</pre>
# 7a.
<pre>
body.T <- c(33.2,34.3,34.8,36.1,35.9,35.1,34,34.2,33.6,35.4,35.3,35.4,33.6,33.7,33.8,32.9,34.8,33.7,34.8,33.7,33.1,36.2,34.3,36.3)
body.R <- c(“Forehead”,”Forehead”,”Forehead”,”Throat”,”Throat”,”Throat”,”Forehead”,”Forehead”,”Forehead”,”Throat”,”Throat”,”Throat”,”Forehead”,”Forehead”,”Forehead”,”Throat”,”Throat”,”Throat”,”Forehead”,”Forehead”,”Forehead”,”Throat”,”Throat”,”Throat”)
my.TB <- data.frame(body.R, body.T)
with(my.TB, Hist(body.T, groups=body.R, scale=”frequency”, breaks=”Sturges”, col=”darkgray”))
RcmdrMisc::Hist(my.TB$body.T, groups=my.TB$body.R, col=c(“blue”,”red”))
</pre>
# 7b.
<pre>
dart.D <- c(4.06,8.89,0.00,10.16,11.43,0.00,7.62,7.62,7.37,9.14, NA,10.67)
tossed.by <- c(“aa”,”aa”,”aa”,”aa”,”aa”,”aa”,”bb”,”bb”,”bb”,”bb”,”bb”,”bb”)
RcmdrMisc::Hist(dart.D, groups=tossed.by, col=c(“blue”,”red”))
</pre>
# 7c.
<pre>
shell.length <- c(14.1,17.2,17.6,8,6.83,6.75,6.3,7.7,7.6,6.1,7.2,4.6,17,13.6,13.5,18.5,15.3,19,6.4,7.5,7,7.3,9.1,9)
mollusk.group <- c(“SeaStar”,”SeaStar”,”SeaStar”,”Snail”,”Snail”,”Snail”,”SandDollar”,”SandDollar”,”SandDollar”,”Conus”,”Conus”,”Conus”,”Starfish”,”Starfish”,”Starfish”,”Starfish”,”Starfish”,”Starfish”,”Seashell”,”Seashell”,”Seashell”,”Seashell”,”Seashell”,”Seashell”)
RcmdrMisc::Hist(shell.length, groups=mollusk.group, col=”blue”)
</pre>
# 9.
<pre>
myData <- read.table(header=TRUE, sep=”,”, text=”
Copper, Hazel, HazelCopper
0.02404672, 0.007185706, 0.02663191
0.06711479, 0.027020958, 0.03181153
0.12196060, 0.037725842, 0.03743693
0.13308991, 0.044762867, 0.03851548
0.13344032, 0.045809398, 0.18787608
0.17537831, 0.060942269, 0.19494708
“)
my.Matrix <- as.matrix(myData)
heatmap(my.Matrix)
</pre>
/MD