diff --git a/Exercise_10.R b/Exercise_10.R new file mode 100644 index 0000000..2f71173 --- /dev/null +++ b/Exercise_10.R @@ -0,0 +1,71 @@ +# Exercise 10 + +## 1. create scatter plot + +# load the package +library(ggplot2) + +# read data +mammals = read.table("/Users/erinlewis/Desktop/Biocomp_tutorial12/mammals.txt", header=TRUE, sep="\t", stringsAsFactors=FALSE) + +# plot of body weight (log body weight (kg)) vs. brain weight (log brain weight (kg)) in mammals +ggplot(data = mammals, + aes(x = log_body, y = log_brain)) + + geom_point() + + xlab("body weight (kg)") + + ylab("brain weight (kg)") + + stat_smooth(method="lm") + + theme_classic() + +## 2. generate 2 figures for data.txt + +# read data +info = read.table("/Users/erinlewis/Desktop/Biocomp_tutorial12/data.txt", header=TRUE, sep=",", stringsAsFactors=FALSE) + +#get mean values for each direction + sumNorth<-0 + sumSouth<-0 + sumWest<-0 + sumEast<-0 + + obsNorth<-0 + obsSouth<-0 + obsWest<-0 + obsEast<-0 + +for(i in 1:nrow(info)){ + if(info$region[i]=="north"){ + sumNorth=sumNorth+info$observations[i] + obsNorth=obsNorth+1 + }else if(info$region[i]=="south"){ + sumSouth=sumSouth+info$observations[i] + obsSouth=obsSouth+1 + }else if(info$region[i]=="west"){ + sumWest=sumWest+info$observations[i] + obsWest=obsWest+1 + }else if(info$region[i]=="east"){ + sumEast=sumEast+info$observations[i] + obsEast=obsEast+1 + } +} + + meanInfo = data.frame(region = c("north", "south", "west", "east"), + means = c(sumNorth/obsNorth, sumSouth/obsSouth, sumWest/obsWest, sumEast/obsEast)) + +# bar plot +ggplot(data = meanInfo, + aes(x = region, y = means)) + + geom_bar(stat = "identity", fill = "orange") + +# scatter plot +ggplot(data = info, + aes(x = region, y = observations)) + + geom_jitter() + + +## The scatter plot and the bar plot tell very different stories. The bar plot only shows the means of each +## population. All the populations have similar means, so they all look very similar on the bar graph. The +## scatter plot, in contrast, shows all the observations individually as points, and you can see that how the +## populations are distributed differently. The north population, for example, is concentrated around +## 15, while the south population has no observations at 15 at all! Thus, it is clear that though the means +## of the populations may be similar, their distribution along the gradient is not. diff --git a/mammals.txt b/mammals.txt new file mode 100644 index 0000000..bb3c4e6 --- /dev/null +++ b/mammals.txt @@ -0,0 +1,54 @@ + body_wt_kg brain_wt_g log_body log_brain +Mammal 10 52.160 440.00 1.72 2.64 +Mammal 11 0.425 6.40 -0.37 0.81 +Mammal 12 465.000 423.00 2.67 2.63 +Mammal 13 0.550 2.40 -0.26 0.38 +Mammal 14 187.100 419.00 2.27 2.62 +Mammal 15 0.075 1.20 -1.12 0.08 +Mammal 16 3.000 25.00 0.48 1.40 +Mammal 17 0.785 3.50 -0.11 0.54 +Mammal 18 0.200 5.00 -0.70 0.70 +Mammal 19 1.410 17.50 0.15 1.24 +Mammal 20 60.000 81.00 1.78 1.91 +Mammal 21 529.000 680.00 2.72 2.83 +Mammal 22 27.660 115.00 1.44 2.06 +Mammal 23 0.120 1.00 -0.92 0.00 +Mammal 24 207.000 406.00 2.32 2.61 +Mammal 25 85.000 325.00 1.93 2.51 +Mammal 26 36.330 119.50 1.56 2.08 +Mammal 27 0.101 4.00 -1.00 0.60 +Mammal 28 1.040 5.50 0.02 0.74 +Mammal 29 521.000 655.00 2.72 2.82 +Mammal 30 100.000 157.00 2.00 2.20 +Mammal 31 35.000 56.00 1.54 1.75 +Mammal 32 0.005 0.14 -2.30 -0.85 +Mammal 33 0.010 0.25 -2.00 -0.60 +Mammal 34 62.000 1320.00 1.79 3.12 +Mammal 35 0.122 3.00 -0.91 0.48 +Mammal 36 1.350 8.10 0.13 0.91 +Mammal 37 0.023 0.40 -1.64 -0.40 +Mammal 38 0.048 0.33 -1.32 -0.48 +Mammal 39 1.700 6.30 0.23 0.80 +Mammal 40 3.500 10.80 0.54 1.03 +Mammal 41 250.000 490.00 2.40 2.69 +Mammal 42 0.480 15.50 -0.32 1.19 +Mammal 43 10.000 115.00 1.00 2.06 +Mammal 44 1.620 11.40 0.21 1.06 +Mammal 45 192.000 180.00 2.28 2.26 +Mammal 46 2.500 12.10 0.40 1.08 +Mammal 47 4.288 39.20 0.63 1.59 +Mammal 48 0.280 1.90 -0.55 0.28 +Mammal 49 4.235 50.40 0.63 1.70 +Mammal 50 6.800 179.00 0.83 2.25 +Mammal 51 0.750 12.30 -0.12 1.09 +Mammal 52 3.600 21.00 0.56 1.32 +Mammal 53 83.000 98.20 1.92 1.99 +Mammal 54 55.500 175.00 1.74 2.24 +Mammal 55 1.400 12.50 0.15 1.10 +Mammal 56 0.060 1.00 -1.22 0.00 +Mammal 57 0.900 2.60 -0.05 0.41 +Mammal 58 2.000 12.30 0.30 1.09 +Mammal 59 0.104 2.50 -0.98 0.40 +Mammal 60 4.190 58.00 0.62 1.76 +Mammal 61 3.500 3.90 0.54 0.59 +Mammal 62 4.050 17.00 0.61 1.23 \ No newline at end of file