1

I'm trying to create hundreds of plots (one plot for each site/program) for time series data (annual water quality averages over the years). I need to plot each site/program combination with each parameter (ANC, DOC, etc). Years run from 1990 to 2017. When a parameter isn't collected, it's stored as a 0, but I can change this to a null if needed. I'd like to do this as one code/script to save time.

I'm more familiar with ggplot2, but I'm open to other packages as well.

Here's an example of my data:

PROGRAM <fctr> SITE <fctr> YEAR <int> ANC <dbl> DOC <dbl> NO3 <dbl> SBC_ALL <dbl> SBC.0 <dbl> SO4 <dbl>

1   VTSSS   VT64    1992    66.753  0   0.803   94.890  0   8.135
2   VTSSS   VT64    1998    64.778  0   0.803   92.783  0   8.230
3   VTSSS   VT43    1995    58.925  0   0.698   91.945  0   8.730
4   VTSSS   VT43    1990    63.240  0   0.885   91.618  0   8.733
5   VTSSS   VT43    1992    66.043  0   0.825   93.873  0   8.758
6   VTSSS   VT43    2004    66.298  0   0.688   95.358  0   8.813

I've tried ggplot, but it only comes up with an empty graph. I'm thinking I need a graphing package where I can subset by PROGRAM and SITE.

I've tried:

ggplot(data = mydata, mapping = aes(x = "year", y = "anc", color = "site")) +
 geom_line() +
 theme_bw()

Expected Results: Want to have a plot for site: VT43 and parameter: ANC over time (1990 to 2017). VT43 and DOC. VT43 and NO3. Etc....

enter image description here

Actual Results:

Empty graph with year on the x axis, anc on the y axis, and no dots or lines for sites.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Scott R
  • 133
  • 2
  • 13
  • 1
    It seems like you're looking for facets, but like there's a problem in how you're plotting before getting to that point. Try using bare column names, as that's what `ggplot` expects – camille Apr 22 '19 at 15:11
  • 1
    Your plot is blank because R is case-sensitive, and `anc` is not `ANC`. – Jon Spring Apr 22 '19 at 15:33
  • Thanks for identifying that for me! I've been working in Oracle recently and cases aren't as sensitive there. I've corrected the column names and the plot works! However, the number of SITES is overwhelming the graph. Is there a way to plot each individual site in one graph (without having to do each one manually)? Maybe using a loop of some sorts? – Scott R Apr 22 '19 at 16:21
  • 1
    @ScottR You may wish to check out the `facet_wrap_paginate` function from the [ggforce package](https://rdrr.io/cran/ggforce/man/facet_wrap_paginate.html). – Z.Lin Apr 23 '19 at 01:54
  • 1
    @ScottR: this might help https://stackoverflow.com/a/52045613/786542 – Tung Apr 23 '19 at 05:08
  • @Z.Lin That looks exactly like what I need. I'll be checking it out today. Thanks. – Scott R Apr 29 '19 at 13:51

0 Answers0