## A very first R tutorial! ## "Let's start in the very beginning, the very best place to start" ## By Liz Mandeville, April 2024 (lmandevi@nmu.edu) ## This is an R script ## Lines that start with "#" or "##" are comments and aren't interpreted as code ## There are R extras called "packages" - we are going to install a couple of packages that are useful install.packages("palmerpenguins") #this is a dataset install.packages("ggplot2") ## You install packages only once, but every time you need them you need to load them with library() library(palmerpenguins) library(ggplot2) ## We will work a lot with this dataset today data(package='palmerpenguins') View(penguins) ## but, usually we bring in data like this, and this is historically one of the things R newbies find hardest ## First: tell R where the file is setwd("~/northernmichigan/teaching/fishecol_BI467_w24/code_n_data/") ## Then, bring it in! Give it a good name to refer to fish <- read.csv("length_weight_example.csv") ## You refer to one column like this: penguins$species fish$length ## Take a mean (average) mean(penguins$body_mass_g) ## uh oh! mean(penguins$body_mass_g, na.rm=TRUE) ## plotting code - this is base R plot(penguins$flipper_length_mm,penguins$body_mass_g) ## Can color-code, but it's messy plot(penguins$flipper_length_mm, penguins$body_mass_g, type="n") points(penguins$flipper_length_mm[penguins$species=="Adelie"],penguins$body_mass_g[penguins$species=="Adelie"], col="deeppink") points(penguins$flipper_length_mm[penguins$species=="Gentoo"],penguins$body_mass_g[penguins$species=="Gentoo"], col="seagreen") points(penguins$flipper_length_mm[penguins$species=="Chinstrap"],penguins$body_mass_g[penguins$species=="Chinstrap"], col="purple") ## Most people prefer ggplot! ggplot(penguins, aes(x=flipper_length_mm, y=body_mass_g, color=species)) + geom_point() ## If we still have time, try replacing a variable and plotting something different! Or try to make these same plots with the fish dataset