Skip to content

Instantly share code, notes, and snippets.

@chenpanliao
Last active April 9, 2020 06:09
Show Gist options
  • Save chenpanliao/4b5c9c5591c087106d471756af511579 to your computer and use it in GitHub Desktop.
Save chenpanliao/4b5c9c5591c087106d471756af511579 to your computer and use it in GitHub Desktop.
# Discussion Figure 2 - Pressure audiograms for Elasmobranchs
setwd("H:/1. Shark Project/9. Data analysis/Behavioral Audiogram Analysis")
#getwd()
library(ggplot2)
library(ggpubr)
library(magrittr)
d <- read.table("Elasmo_audiogramms.txt",header=T,na.strings=c("NA","NaN",""),sep="\t")
#d <- d[d$study == 'CN' | d$study == 'ambient', ]
# First prepare a aggregated dataframe for average, upper boundary (average + 1SD) and lower boundary (average - 1SD)
# grouping by frequency and study.
d.agg <- aggregate(threshold ~ frequency + species, data = d, FUN = mean)
d.agg$upper <- aggregate(threshold ~ frequency + species, data = d, FUN = function(x) {mean(x) + sd(x)})$threshold
d.agg$lower <- aggregate(threshold ~ frequency + species, data = d, FUN = function(x) {mean(x) - sd(x)})$threshold
d.agg$N <- aggregate(threshold ~ frequency + species, data = d, FUN = length)$threshold
ggplot(d.agg, aes(frequency, threshold, color = species)) +
geom_line(aes(group = species, linetype = species)) + # add line
geom_point(aes(shape = species, color = species, fill = species)) + # add average (d.agg$frequency) point
geom_errorbar(aes(ymax = upper, ymin = lower)) + # add error bar (d.agg$lower and d.agg$upper)
# redefine three linetypes; google "R plot lty" to know the details
scale_linetype_manual(values = c(1,1,1,1,2,1,1,1)) +
# redefine three color
scale_color_manual(values = c("grey46", "darkorchid1", "steelblue2", "red","red", "black", "black", "green")) +
# redefine three shape and fill
scale_shape_manual(values = c(21,21,21,23,23,22, 22, 21, 21)) + # see ?pch
scale_fill_manual(values = c("grey46", "darkorchid1", "steelblue2", "red","white", "black", "white", "green")) + # "white" make orange point empty
# reforage y axis
scale_y_continuous(
name = "Sound Pressure Level (dB re 1uPa)", # y label
breaks = seq(90, 200, 5), # tick definition
limits = c(90, 180) # set limits
) +
# reforage x axis
scale_x_continuous(
trans = "log",
name = "Frequency [Hz]", # x label
breaks = c(seq(1,9,1), seq(10,90,10), seq(100,900,100),seq(1000,9000,1000)), # tick definition
labels = c(1,rep("", 8), 10,rep("", 8), 100,rep("", 8), 1000, rep("", 8)),
limits = c(9, 1500) # limits
) +
theme_pubr(base_size = 10, border = T, legend = "right") + # this theme from ggpubr package; highly recommanded
theme(legend.text = element_text(size = 10)) # make texts in legend 10 pt to adapt other texts
#scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
#labels = trans_format("log10", math_format(10^.x))) +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment