5

I need to plot mean monthly temperature and have the month abbreviated on the x axis, and I need to add 95% confidence intervals but not sure how. Any visual of CI's would be good.

Then I need to plot

I got Date...Time split out into separate columns but I can't get the X axis to display abbreviated month with month.abb in ggplot.

I was given the below dataset (shortened for stackflow):

# Data
CleanTempSal = data.frame(
  stringsAsFactors = F,
    Date...Time = c(
        "1/31/2017 20:00",
        "1/31/2017 21:00",
        "1/31/2017 22:00",
        "1/31/2017 23:00",
        "2/1/2017 0:00",
        "2/1/2017 1:00",
        "2/1/2017 2:00",
        "2/1/2017 3:00",
        "3/21/2017 10:00",
        "3/21/2017 11:00",
        "3/21/2017 12:00",
        "3/21/2017 13:00"),

    Temp..C. = c(14.87, 14.77, 15.08, 15.08, 
                  14.96, 14.87, 15.05, 15.05, 
                  18.87, 19.32, 19.97, 20.44),

    Salinity.psu. = c(14.58, 14.52, 14.44, 14.46, 
                      14.56, 14.67, 14.78, 14.88, 
                      18.78, 18.81, 19.41, 19.16),

    Conduc.mS.cm. = c(19.33, 19.21, 19.26, 19.28,
                      19.34, 19.44, 19.66, 19.78, 
                      26.67, 26.96, 28.14, 28.09)
    )
Date...Time   Temp..C.  Salinity.psu.   Conduc.mS.cm.
1/31/2017 20:00 14.87   14.58   19.33
1/31/2017 21:00 14.77   14.52   19.21
1/31/2017 22:00 15.08   14.44   19.26
1/31/2017 23:00 15.08   14.46   19.28
2/1/2017 0:00   14.96   14.56   19.34
2/1/2017 1:00   14.87   14.67   19.44
2/1/2017 2:00   15.05   14.78   19.66
2/1/2017 3:00   15.05   14.88   19.78
3/21/2017 10:00 18.87   18.78   26.67
3/21/2017 11:00 19.32   18.81   26.96
3/21/2017 12:00 19.97   19.41   28.14
3/21/2017 13:00 20.44   19.16   28.09

And the code.

library(tidyverse)
library(ggplot2)
library(lubridate)

# convert date column to date class
CleanTempSal$Date...Time <- as.POSIXct(CleanTempSal$Date...Time, format = "%m/%d/%y %H:%M")

#Add Month Column to data set
CleanTempSal <- CleanTempSal %>% mutate(month = month(Date...Time))
CleanTempSal <- CleanTempSal %>% mutate(month2 = month.abb[month])
CleanTempSal <- CleanTempSal %>% mutate(year = year(Date...Time))
CleanTempSal <- CleanTempSal %>% mutate(hour = hour(Date...Time))


#group by month and take the mean of that month
a <- CleanTempSal %>%
  group_by(month) %>%
  summarise(month_mean = mean(Temp..C.))

#plot mean monthly temp
ggplot(a, aes(month, month_mean)) +
  geom_point(aes(color = month_mean)) + 
  geom_line(aes(color = month_mean)) +
  scale_color_gradient("Temp", low = "blue", high = "red4") +
  labs(x = "Month of 2017",
       y = "Water Tempearture (C)",
       title = "Monthy Mean Water Temperature",
       subtitle = "NCBS Dock - Cedar Key, FL")

gives me this

The data provided will not produce the same plot as I have shortened it for simplicity. It will only give the first 3 months and the means will be different, but accomplish the same goals.

image of output

Tung
  • 26,371
  • 7
  • 91
  • 115
Johnny5ish
  • 295
  • 1
  • 2
  • 12
  • Thank you @Rui Barradas – Johnny5ish Mar 28 '20 at 20:01
  • 1
    One quick problem I noticed, you need `%m/%d/%Y %H:%M` converting date/time, capital 'Y' as year is 4 digits not 2 – Ben Mar 28 '20 at 21:25
  • I apologize, as I said I am very new at this, it importet into R like this... So the lower case y is correct, The other data is from excel. I should have been more clear. Date...Time Temp..C. Salinity.psu. Conduc.mS.cm. 1 1/13/17 0:00 14.65 24.19 30.52 2 1/13/17 1:00 14.93 24.23 30.76 3 1/13/17 2:00 14.99 24.28 30.86 4 1/13/17 3:00 14.65 24.35 30.70 5 1/13/17 4:00 14.68 24.35 30.72 6 1/13/17 5:00 14.65 24.35 30.70 – Johnny5ish Mar 28 '20 at 21:38

1 Answers1

2

Here is one way to approach this:

To get the month abbreviations, I might consider keeping month as POSIXct. By using floor_date you can get the month for each time point and store in the desired format. When plotting, you can use scale_x_datetime and specify the label you would like to use on the xaxis. In this case, %b will provide the month abbreviation.

To do the 95% confidence interval, there are different approaches to consider. One way is to compute manually the 95% CI. Note that assumptions are made here (based on Student t distribution). In this case, I used geom_ribbon with some transparency (alpha .2) to show the interval across points. Alternative to this, you could use stat_summary, which would compute mean and 95% CI and display in ggplot.

#group by month and take the mean of that month
a <- CleanTempSal %>%
  group_by(month = floor_date(Date...Time, unit = "month")) %>%
  summarise(month_mean = mean(Temp..C.),
            sd = sd(Temp..C.),
            n = n()) %>%
  mutate(se = sd / sqrt(n),
         lower.ci = month_mean - qt(1 - (.05/2), n - 1) * se,
         upper.ci = month_mean + qt(1 - (.05/2), n - 1) * se)

#plot mean monthly temp
ggplot(a, aes(x = month, y = month_mean)) +
  geom_point(aes(color = month_mean)) + 
  geom_line(aes(color = month_mean)) +
  geom_ribbon(aes(ymin = lower.ci, ymax = upper.ci), alpha = 0.2) +
  scale_color_gradient("Temp", low = "blue", high = "red4") +
  scale_x_datetime(date_breaks = "1 month", date_labels = "%b") +
  labs(x = "Month of 2017",
       y = "Water Tempearture (C)",
       title = "Monthy Mean Water Temperature",
       subtitle = "NCBS Dock - Cedar Key, FL")

Plot

plot with 95% CI and xaxis labels with months

Edit (4/16/20):

If you have multiple years of data, when calculating SD and SE you should group by both month and year:

group_by(month = floor_date(Date...Time, unit = "month"), year)

In addition, I revised the ggplot to show error bars instead of ribbon. There are some minor changes made to get width of error bars including use of as.Date(month) and scale_x_date.

#group by month and take the mean of that month
a <- CleanTempSal %>%
  group_by(month = floor_date(Date...Time, unit = "month"), year) %>%
  summarise(month_mean = mean(Temp..C.),
            sd = sd(Temp..C.),
            n = n()) %>%
  mutate(se = sd / sqrt(n),
         lower.ci = month_mean - qt(1 - (.05/2), n - 1) * se,
         upper.ci = month_mean + qt(1 - (.05/2), n - 1) * se)

#plot mean monthly temp
ggplot(a, aes(x = as.Date(month), y = month_mean)) +
  geom_point(aes(color = month_mean)) + 
  geom_line(aes(color = month_mean)) +
  #geom_ribbon(aes(ymin = lower.ci, ymax = upper.ci), alpha = 0.2) +
  geom_errorbar(aes(ymin = month_mean - se, ymax = month_mean + se), width = 1) +
  scale_color_gradient("Temp", low = "blue", high = "red4") +
  scale_x_date(date_breaks = "1 month", date_labels = "%b %y") +
  labs(x = "Month",
       y = "Water Tempearture (C)",
       title = "Monthy Mean Water Temperature",
       subtitle = "NCBS Dock - Cedar Key, FL")

Plot

plot with error bars

Data

CleanTempSal <- structure(list(Date...Time = structure(c(1485914400, 1485918000, 
1485921600, 1485925200, 1485928800, 1485932400, 1485936000, 1485939600, 
1490108400, 1490112000, 1490115600, 1490119200), class = c("POSIXct", 
"POSIXt"), tzone = ""), Temp..C. = c(14.87, 14.77, 15.08, 15.08, 
14.96, 14.87, 15.05, 15.05, 18.87, 19.32, 19.97, 20.44), Salinity.psu. = c(14.58, 
14.52, 14.44, 14.46, 14.56, 14.67, 14.78, 14.88, 18.78, 18.81, 
19.41, 19.16), Conduc.mS.cm. = c(19.33, 19.21, 19.26, 19.28, 
19.34, 19.44, 19.66, 19.78, 26.67, 26.96, 28.14, 28.09), month = c(1, 
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), month2 = c("Jan", "Jan", "Jan", 
"Jan", "Feb", "Feb", "Feb", "Feb", "Mar", "Mar", "Mar", "Mar"
), year = c(2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017), hour = c(20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
10L, 11L, 12L, 13L)), class = "data.frame", row.names = c(NA, 
-12L))
Ben
  • 28,684
  • 5
  • 23
  • 45
  • Thank you so much @Ben!!! People are really awesome on this community. How hard would it be to just have them pop up as confidence interval bars for each point instead of a ribbon? – Johnny5ish Mar 28 '20 at 23:14
  • 1
    I would use standard error for bars...you could replace `geom_ribbon` with this: `geom_errorbar(aes(ymin = month_mean - se, ymax = month_mean + se))` – Ben Mar 28 '20 at 23:39
  • Ben I used that line and was informed that I calculated confidence intervals for the whole data set instead of by month. any suggestions? Also when I tried to use the bars instead of ribbon it showed up as a line and i couldn't get the top and bottom hats of the error bars to show up. – Johnny5ish Apr 16 '20 at 18:04
  • 1
    @Johnny5ish The SD and SE are calculated by month, but if you have multiple years of data it would be grouping years together for a given month. Could that be the case? If so, then you can `group_by` both month and year. – Ben Apr 16 '20 at 19:13
  • 1
    @Johnny5ish See edited answer above. This should `group_by` both month and year. Also has example with error bars. Hope this helps - if still a problem, let me know. – Ben Apr 16 '20 at 19:38