Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions exercise9script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# ggplot2 and cowplot load
library(ggplot2)
library(cowplot)

# set up directory
setwd("/Users/7907blueyes/Desktop/Biocomputing/R/Exercise09")

# QUESTION ONE
# load in data
predationrisk <- read.csv("predationrisk.csv", header = TRUE, stringsAsFactors = FALSE)
# scatter plot with trend line
ggplot(predationrisk, aes(x = Group.Size, y = Predation.Risk)) + geom_point() +
xlab("Group Size") + ylab("Predation Risk") + stat_smooth(method = "lm")

# QUESTION TWO
# load in data (separated by commas)
data <- read.csv("data.txt", header = TRUE, stringsAsFactors = FALSE)
# calculating mean of averages
avgnorth <- mean(data[data$region == "north",2])
avgsouth <- mean(data[data$region == "south",2])
avgwest <- mean(data[data$region == "west",2])
avgeast <- mean(data[data$region == "east",2])
# saving averages in data frame
avgdata <- data.frame(region = c("north", "south", "east", "west"), average = c(avgnorth, avgsouth, avgeast, avgwest))
# bar plot
ggplot(avgdata, aes(x = region, y = average)) + geom_bar(stat = "identity") + theme_bw()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use geom_stat like in the lecture slides

# scatter plot with observations
ggplot(data, aes(x = region, y = observations)) + geom_jitter()

# Do the bar and scatter plots tell you different stories? Yes, the two graphs tell different
# stories about the data. In the bar graph, the region averages are incredibly similar leading
# one to believe that the observations are fairly close. In the scatter plots, it is clear that
# the observations are concentrated at different points and that the regions are not altogether
# that similar.


11 changes: 11 additions & 0 deletions predationrisk.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Group Size,Predation Risk
1,0.7
2,0.35
3,0.24
4,0.23
5,0.2
6,0.17
7,0.16
8,0.14
9,0.13
10,0.1