mcglinnlab / shark-ray-div

Global patterns of shark and ray diversity
0 stars 1 forks source link

correlation plotting function error #29

Closed EmmalineSheahan closed 2 years ago

EmmalineSheahan commented 6 years ago

So I wrote a function that would do basic plotting between two variables by pulling them out of the big data frame I made:

make_plot <- function(x, y, z, w, v) {
   stats <- vector("list", length = 6)
   pdf(v)
   for (i in 1:6) {
        lin <- lm(data_list[[i]]$y ~ data_list[[i]]$x) 
        plot(data_list[[i]]$x, data_list[[i]]$y, 
             main = paste('resolution =', res(species_richness[[i]])), 
             xlab = z, ylab = w)
        abline(lin, col = 'red')
        stats[[i]] <- summary(lin)
}
dev.off()
return(stats)
}

but it won't work at all; R specifically seems to have a problem with this: data_list[[i]]$x as if it doesn't recognize it's supposed to be plugging something in for x

EmmalineSheahan commented 6 years ago

I figured it out, here's the new code which works:

# make_plot function plots two variables against one another
# x = x axis variable, must be chosen from data_list, in character form
# y = y axis variable, must be chosen from data_list, in character form
# z = x axis label as character string
# w = y axis label as character string
# v = pdf file
# output are statistics summary, name and save accordingly
make_plot <- function(x, y, z, w, v) {
   load('./data/data_list.Rdata')
   load('./data/raster/species_richness.Rdata')
   stats <- vector("list", length = 6)
   pdf(v)
   for (i in 1:6) {
        column1 <- substitute(x)
        column2 <- substitute(y)
        lin <- lm(eval(column2, data_list[[i]]) ~ eval(column1, data_list[[i]])) 
        plot(eval(column1, data_list[[i]]), eval(column2, data_list[[i]]), 
             main = paste('resolution =', res(species_richness[[i]])), 
             xlab = z, ylab = w)
        abline(lin, col = 'red')
        stats[[i]] <- summary(lin)
    }
dev.off()
return(stats)
}