0

I'm looking for help navigating and visualizing a HUGE field study dataset in R. I would like to automate visualizing subsets of the data. My field study involves various samples (numeric) taken from different ponds (factor) in multi-pond systems (factor) across seasons (factor). I want to graph how various numeric measurements (x) vary with depth (y) for each pond system with point shape according to pond (within the system being considered) and color by season.

To do this, I think I need to compress the data for each pond system using "nest:"

    comp_nested <- comp %>% group_by(System) %>% nest()        

This is what the nested dataframe looks like

Where I'm getting stuck is at accessing each system's nested data to graph parameters of interest in a for loop:

for (i in comp_nested$System) {
  
unnested <- unnest(comp_nested[2], as_df)
str(unnested)

scatter_fun = function(x, y) {
     ggplot(unnested, aes(x = .data[[x]], y = .data[[y]], color=Season, shape=Pond, size=0.5) ) +
    scale_y_reverse()+
          geom_point() +
  theme(axis.text = element_text(size = 10), panel.background=element_rect(fill="white", color="black"))+
  theme(legend.key=element_rect(fill="white"), legend.title= element_text(size=10), legend.text=element_text(size=10))+
  guides(size=FALSE)+
  guides(color = guide_legend(override.aes = list(size = 2)), shape=guide_legend(override.aes = list(size = 2)))
}

scatter_fun(x="Ammonia_N", y="Depth_in")
}

I can get the code to work for ALL the systems, but I can't create a separate graph for each system: This graph shows how ammonia varies with depth for all ponds in all systems across seasons.

Thanks in advance for any help you can provide!

  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. We don't need your actual data, feel free to test with a built-in dataset. Take out unnecessary code like calls to `theme()` or `guides()` to make the problem more clear. – MrFlick Aug 28 '21 at 01:13

1 Answers1

0

One option to achieve your desired result would be to add a data argument to your function a to loop over the data column of your nested dataset.

Making use of the mpg dataset as example data:

library(ggplot2)
library(tidyr)
library(dplyr)

scatter_fun <- function(.data, x, y) {
  ggplot(.data, aes(x = .data[[x]], y = .data[[y]], color = manufacturer, shape = cyl)) +
    scale_y_reverse() +
    geom_point(size = 2) +
    theme(axis.text = element_text(size = 10), panel.background = element_rect(fill = "white", color = "black")) +
    theme(legend.key = element_rect(fill = "white"), legend.title = element_text(size = 10), legend.text = element_text(size = 10)) +
    guides(color = guide_legend(override.aes = list(size = 2)), shape = guide_legend(override.aes = list(size = 2)))
}

mpg_nested <- mpg %>% 
  mutate(cyl = factor(cyl)) %>% 
  nest(data = -class)

foo <- lapply(mpg_nested$data, function(x) scatter_fun(x, "cty", "hwy"))

foo[[1]]

foo[[2]]

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you so much! I've gotten this working overall, but I'm struggling to convince R to adjust color, shape, and size within the ggplot within the function (e.g. color=season and shape=location while increasing the size of the points). Nothing I've tried changes the aesthetics of the output graph... Any advice? – Rachel Tenney Oct 03 '21 at 01:25
  • Hm. Hard to tell what's the issue without a snippet of your real data and the code you tried. But I just made an edit where I switched to the `mpg` dataset and added 1) color and shape aes as well as 2) changed the size of the points. – stefan Oct 03 '21 at 06:43
  • Stefan - thank you so much! This worked out so well and accomplishes exactly what I was attempting. – Rachel Tenney Oct 03 '21 at 17:59