Â
These exercises cover base plotting for Plotting in R.
Exercise 1 - Base Plots
## Time Control1 Control2 Treatment1 Treatment2
## Min. : 0.0 Min. :2.000 Min. :3.000 Min. : 3.00 Min. : 4.0
## 1st Qu.: 2.5 1st Qu.:3.500 1st Qu.:3.500 1st Qu.: 7.00 1st Qu.: 8.5
## Median : 5.0 Median :5.000 Median :4.000 Median :12.00 Median :13.0
## Mean : 5.0 Mean :4.909 Mean :4.727 Mean :10.36 Mean :11.0
## 3rd Qu.: 7.5 3rd Qu.:6.500 3rd Qu.:6.000 3rd Qu.:14.50 3rd Qu.:14.0
## Max. :10.0 Max. :7.000 Max. :7.000 Max. :15.00 Max. :15.0
Produce a histogram of the values for control and treatment
samples
Put these in the same plot with same X scale limits by modifying
the parameters.
Make a barplot of values in control sample 1 for every time
point. Add a title and color scheme from a palette of your choice.
Use a for loop to plot all 4 samples on a single plot
Make a barplot of mean values in control and treatment samples
side by side for every time point
Create a line graph showing control and treatment samples against time. Use different
Exercise 2 - A working example
## salmon_id_number common_name length_millimeters weight_grams
## 1 35032 Chinook salmon 147 25.6
## 2 35035 Sockeye salmon 121 NA
## 3 35036 Sockeye salmon 112 NA
## 4 35037 Steelhead 220 NA
## 5 35038 Steelhead 152 NA
## 6 35033 Chinook salmon 444 1011.7
## salmon_id_number common_name length_millimeters weight_grams
## Min. :35032 Length:100 Min. : 90.0 Min. : 20.70
## 1st Qu.:35058 Class :character 1st Qu.:147.0 1st Qu.: 25.55
## Median :35088 Mode :character Median :179.5 Median : 28.60
## Mean :35089 Mean :186.4 Mean : 142.14
## 3rd Qu.:35120 3rd Qu.:215.2 3rd Qu.: 46.85
## Max. :35147 Max. :444.0 Max. :1011.70
## NA's :81
barplot(table(salmon$common_name), col="salmon", ylab = "Number Caught", main="Observed Numbers of Salmon Species")
plot(salmon$length_millimeters, salmon$weight_grams, col="salmon", ylab = "Weight (g)", xlab = "Length (mm)", pch=5, main="Correlation between weight and length of salmon", las=2)
par(mfrow=c(2,2))
salmon_types <- unique(salmon$common_name)
for (i in 1:4){
toplot <- salmon[salmon$common_name %in% salmon_types[i],]
hist(toplot$length_millimeters, xlab = "length(mm)", main = paste("Salmon Length (mm)\n",salmon_types[i]), xlim= c(50,450), col="salmon")
}
par(mfrow=c(1,1))
library(vioplot)
vioplot(length_millimeters~common_name, data = salmon, use.cols=F, main="Distribution of lengths", col="salmon", cex.axis=0.8)
pheatmap(salmon_mercury, color = viridis(20), cluster_cols = F, main="Mercury levels (ppm) in Salmon species over time")
pheatmap(salmon_mercury, scale ="row", cluster_cols = F, main="Z-scores of Mercury levels (ppm) in Salmon species over time")
library(RColorBrewer)
my_pal <- colorRampPalette(c("Blue","White","Red"))(60)
pheatmap(salmon_mercury, scale ="row", breaks = seq(-1.5,1.5,0.05), color = my_pal, cluster_cols = F, main="Z-scores of Mercury levels (ppm) in Salmon species over time")