test_df <- read.csv("C:\\Users\\jdesw\\Desktop\\AEON LAB\\R work\\Test\\sdi_richness.csv")Test
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
- input CSV
- find CSV file and copy as path
- go to box, make ssuwre there is {r} and paste path
- in box, write “read.csv” and replace \ with \\.
- Have the path in the ()
- make a new chunk via ctl-alt-i
- make a box plot compairing species diversity and habitat type
- open and/or install packages
library(ggplot2)
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.2
✔ lubridate 1.9.4 ✔ tibble 3.3.0
✔ purrr 1.1.0 ✔ tidyr 1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)make a new chunk
for GG plot
a) first but in file
b) then doe aesthetic for the inputted file
c) change colour of boxes by fill.
d) outside of brackets do a + then press “enter” and add the geom_boxplot to specify the type of plot
e) now we add figure caption, use quatation amrks for adding labelsand we can use spaces here.
f) add theme
- add theme for title, meaning we adjust the title location from to centered (on a scale of [0, 1].
ggplot(test_df, aes(x = Habitat, y = Mean.SDI, fill = Habitat))+
geom_boxplot()+
labs(
x = "Habitat Type", y = "Mean SDI", title = "SDI by Habitat"
)+
theme_classic()+
theme(plot.title = element_text(hjust = 0.5))- Now save
- File -> save all
Now we want to do a scatter plot relating the occurence of scaridae (parrotfish) with pomicentridae (damselfish)
- new chunk (and run libraries again in case you closed the document)
- New data frame (new excel sheet), we add a %>% (loop) meaning we can modify test_df but the results of the modifications are in the new spreadsheet “Parrot”. Anything after %>% is a modification applied.
- Select the columns I want to use (specified after the loop).
Parrot <- test_df %>%
select(Site, TotalSM, Habitat, Pomacentridae, Scaridae)- now we normalize the species counts by total square meters (*100 means for 100 square meters)
- Use the Mutate function, this will new columns to the Parrot spreadsheet
Parrot <- Parrot %>%
mutate(Damsel = Pomacentridae/TotalSM*100, Parrot = Scaridae/TotalSM*100)- now we graph
- make new chunk, copy and paste previous ggplot then make necessary modifications.
ggplot(Parrot, aes(x = Damsel, y = Parrot))+
geom_point()+
labs(
x = "Damselfish", y = "Parrotfish", title = "Parrotfish by Damselfish"
)+
theme_classic()+
theme(plot.title = element_text(hjust = 0.5))add colour by site and other things
ggplot(Parrot, aes(x = Damsel, y = Parrot, colour = Habitat)) + geom_point(size = 3) + geom_smooth(method = lm, se=F)+ facet_wrap(~Habitat)+ scale_colour_brewer(palette = "Dark2") + labs( x = "Damselfish", y = "Parrotfish", title = "Parrotfish by Damselfish" ) + theme_classic() + theme(plot.title = element_text(hjust = 0.5))`geom_smooth()` using formula = 'y ~ x'save
file -> save all