How to add errorbar around greenhouse gas data point using ggplot2

Hi everyone,
I would like to create a plot with time (Julian day) on the x-axis and CO₂ flux (mg CO2/m-2/day-1) on the y-axis. The flux should be distinguished by three cropping histories (corn, soybean, and fallow). I would also like to display three dotted points in each time period to indicate the cropping histories and include error bars around the points.
I have been struggling with this and would greatly appreciate any guidance or example code.

Packages

library(tidyverse)
library(janitor)

Data

ghg_data <-
  structure(
    list(
      cropping_history = structure(
        c(
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          3L,
          3L,
          1L,
          1L,
          2L,
          2L,
          3L,
          3L,
          2L,
          2L,
          1L,
          1L
        ),
        levels = c("Corn", "Fallow", "Soybean"),
        class = "factor"
      ),
      jday = c(
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        125,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        135,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        142,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        148,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        155,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        162,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        169,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        176,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        184,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        197,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        205,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        209,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        219,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225,
        225
      ),
      co2_flux_mg_co2_m_2_day_1 = c(
        17736.55530336,
        17146.79313696,
        4513.75291584,
        8431.54564896,
        23331.95712864,
        631.93149216,
        6446.84956416,
        25098.81005088,
        25964.21283264,
        23526.52921152,
        11628.42923232,
        13289.9919264,
        11024.82609696,
        15642.9566496,
        9732.5967312,
        10936.30473504,
        14361.5262816,
        23706.0815616,
        21528.56252736,
        19669.65195168,
        9024.27373728,
        9892.79453952,
        12555.1657584,
        11211.3749808,
        7811.66796768,
        10725.83835264,
        15800.11248672,
        13798.87568352,
        12716.0099856,
        15228.86832,
        7112.3187888,
        13255.12333152,
        16075.52495424,
        9826.74573984,
        5015.48804064,
        19525.99486176,
        1103.665176,
        489.1869936,
        -63.61522272,
        352.52643744,
        364.80839616,
        649.49887584,
        -180.69308928,
        -26.88342048,
        104.52973536,
        -297.39070944,
        16.50269376,
        127.87686432,
        3570.01937568,-151.75633824,
        57.30313248,
        95.02357536,
        -646.15270752,
        -363.7056816,
        10308.479904,
        25106.03473248,
        6091.69942656,
        8598.47381856,
        11377.1624112,
        16276.37110272,
        10513.92703392,
        14968.13336352,
        30319.5170736,
        16812.89877312,
        11532.15084384,
        16996.40568576,
        12215.14942752,
        11587.32459648,
        22181.44559616,
        16873.8142464,
        14918.967504,
        18439.6689216,
        3349.17226656,
        13969.03594752,
        -4780.15354368,
        687.21931872,
        6840.8228592,
        5965.41959712,
        11941.02979776,
        6475.13989632,
        6356.61709344,
        3453.62595264,
        3338.63944128,
        2150.40746592,
        14279.43108384,
        9682.51828032,
        93.27444192,
        3815.27830368,
        17785.75918752,
        5951.19838176,-3225.70626048,
        1418.66129376,
        831.48480288,
        -624.32656416,
        12386.94475104,-7502.94591552,
        23844.8714976,
        13157.2859328,
        25395.02199648,
        32597.83942848,
        18441.87435072,
        17499.20550048,
        28340.10641376,
        24982.79687424,
        20016.7408656,
        36962.2315584,
        30162.8555568,
        17570.69182368,
        12233.51532864,
        17832.52949472,
        18485.8688592,
        13518.02569248,
        17331.40276416,
        16514.59547232,
        12605.24420928,
        15172.9720992,
        18739.37913408,
        15584.55080256,
        22720.44486816,
        14242.24298592,
        4244.23426752,
        17335.66152384,
        17522.09633376,
        15861.82647744,
        1572.24281472,
        2565.37036224,
        1010.84702976,
        675.16550784,
        -505.76573664,
        862.51290912,
        -430.97126976,
        -5800.35463488,-1826.74173024,
        -192.63282624,
        229.25055456,
        22221.4094928,
        19269.97496064,
        21604.00341312,
        31834.228608,
        22103.1528624,
        29338.02506592,
        22730.9396688,
        29750.36426208,
        20075.52695904,
        12946.59140256,
        15988.6006272,
        7671.8513664,
        9895.1520672,
        19656.60950016,
        15711.89532192,
        25093.6006752,
        15498.15882048,
        16666.80810624,
        17584.41871872,
        15850.68525792,
        18255.70571328,
        19659.72752064,
        21218.09134176,
        17022.07231776,
        19175.825952,
        16014.15318528,
        19402.30070784,
        992.93742432,
        2485.93688928,
        -1589.88624768,
        986.96755584,
        4007.53088352,
        5796.93241728,
        2366.72964288,
        -814.98210912,
        3741.28235424,
        1717.80113664,-1549.61815392,
        422.33967648,
        4724.44744608,
        2971.16932032,
        -804.10706208,
        4738.40248896,
        1692.13450464,
        3025.20233376,
        11469.22006464,
        27645.16809312,
        4838.74951392,
        7602.41837376,
        11205.89943264,
        10936.6469568,
        13479.5447568,
        12310.51522464,
        12156.2872848,
        7956.16159968,
        9841.00497984,
        8199.89954208,
        18912.42927072,
        11466.06401952,
        8339.60206944,
        8482.04237088,
        17701.53460992,
        17800.20855072,
        -547.66888992,
        -228.5280864,
        127.64871648,
        53.08239744,-1669.77601632,
        532.23088608,
        691.51610304,
        -629.72606304,
        -23.04293184,-216.740448,
        -338.57139456,
        8982.97897824,
        15557.2110864,
        12669.12560448,
        8252.44959456,
        4973.20464096,
        11905.8950304,
        10045.3493952,
        8376.6380688,
        12626.38590912,
        4268.0757168,
        9767.16112896,
        7423.47441792,
        6814.47178368,
        5034.72850848,
        6337.33860096,
        7823.1894336,
        4098.75199488,
        6771.54196512,
        6075.80512704,
        6466.73645088,
        8172.10353024,
        6527.19562848,
        4079.32140384,
        13706.20963584,
        5676.66048096,
        4828.48286112,
        8697.48998112,
        3134.10490272,
        11565.9547488,
        7950.34382976,
        5419.42379136,
        6948.4325904,
        6978.62415456,
        9701.87282208,
        6608.41625952,
        4752.77580288,
        5060.3190912,
        11189.1305664,
        8284.8085632,
        4551.3592848,
        5260.74696864,
        4765.20986016,
        3301.03307232
      )
    ),
    row.names = c(NA, -252L),
    class = c("tbl_df", "tbl", "data.frame")
  )

Visualization

ggplot(data = ghg_data,aes(x = jday,y = co2_flux_mg_co2_m_2_day_1,colour=cropping_history,group=cropping_history))+
  stat_summary(geom = 'errorbar',fun.data = mean_se,linewidth=.2)+
  stat_summary(geom = 'line',fun = mean)+
  stat_summary(geom = 'point',fun = mean)+
  labs(x='Julian days',y='CO2 flux (mg CO2/m^-2/day^-1')

I will greatly appreciate it if someone can help me to improve this visualization!

You don't say what you don't like about the plot. I think it's too cluttered and it is much easier to read if you add

+ facet_wrap(~cropping_history, ncol =1)

to the ggplot code.

1 Like

Thank you very much for your feedback. Please what is your take if I also add letters to each timepoint to show significant difference?

There is room on the faceted plot to add markings for "significance", but I would be very cautious about doing that. How will you calculate the significance? If it is by pairwise comparisons, that's a lot of comparisons and you'll need to account for that.
Also, your data show some behavior that probably needs to be explained. Open a plot window with either the windows() function (on a Windows computer) or the x11() function (on Linux/mac). Drag the plot window edges to make it very wide and make a box plot with

ggplot(ghg_data, aes(x = factor(jday), y = co2_flux_mg_co2_m_2_day_1, 
                     fill = cropping_history)) + 
  geom_boxplot()

That will highlight that your variance changes a lot from day to day, some of the distributions are very skewed, and there is a lot of correlation between the cropping_history groups. The correlation between the daily means can be calculated with

Avgs <- ghg_data |> group_by(jday, cropping_history) |> 
  summarize(Avg = mean(co2_flux_mg_co2_m_2_day_1)) |> 
  pivot_wider(names_from = cropping_history, values_from = Avg)
cor(Avgs[, 2:4])

The correlation values vary from 0.76 to 0.86. Do you expect that much correlation?
On days 142 and 197, all the means drop to nearly zero with very small variance compared to other days. Is that surprising?

All of my comments, except for the skewed distributions, are also visible in the plots with error bars, but I think the box plot highlights them. I'm not saying the box plot is a good way to show the data. It's ugly and the x axis disguises the variation is time steps between measurements. But before jumping to calculating significance values, you should think about what the scatter in the data means.
I don't know enough about your data to say more. I hope my comments help you.

Thank you for your feedback @FJCC, at day 142 and 197 a lot of the datapoints were below zero (negative) and at that period the soil was serving as a sink for CO2, I do not expect that much correlation between the cropping history group. I will have to dig deeper into the data.