r - 用置信区间在 ggplot 中绘制月平均温度

标签 r date ggplot2 confidence-interval

我需要绘制月平均温度并在 x 轴上缩写月份,我需要添加 95% 的置信区间,但不确定如何添加。任何 CI 的视觉效果都会很好。

然后我需要绘制

我收到了 Date...Time拆分为单独的列,但我无法让 X 轴以 month.abb 显示缩写的月份在 ggplot .

我得到了以下数据集(为 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

和代码。
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")

给我这个

提供的数据不会产生与我为简单起见将其缩短的相同的图。它只会给前 3 个月,手段会有所不同,但可以实现相同的目标。

image of output

最佳答案

这是解决此问题的一种方法:

要获得月份缩写,我可能会考虑将月份保留为 POSIXct .通过使用 floor_date您可以获取每个时间点的月份并以所需格式存储。绘图时,您可以使用 scale_x_datetime并指定您想在 xaxis 上使用的标签。在这种情况下,%b将提供月份缩写。

要计算 95% 置信区间,需要考虑不同的方法。一种方法是手动计算 95% CI。请注意,此处进行了假设(基于学生 t 分布)。在这种情况下,我使用了 geom_ribbon使用一些透明度(alpha .2)来显示点之间的间隔。除此之外,您可以使用 stat_summary ,这将计算均值和 95% CI 并显示在 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 with 95% CI and xaxis labels with months

编辑 (4/16/20):

如果您有多年的数据,在计算 SD 和 SE 时,您应该按月份和年份分组:
group_by(month = floor_date(Date...Time, unit = "month"), year)

另外我修改了ggplot显示误差线而不是功能区。为获取误差线的宽度做了一些小改动,包括使用 as.Date(month)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 with error bars

数据
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))

关于r - 用置信区间在 ggplot 中绘制月平均温度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60905953/

相关文章:

java - 如何使用java将日期和时间发送到SQL

r - 使用带有 xml2 的管道运算符编写 XML

r - R 中的信息仪表板与 ggplot2

r - 检查日期是否在R中的两个日期之间

sql-server-2008 - 如何检索之前的日期并在查询中使用它?

vba - 命名范围的数据类型

r - 在字符串上使用 eval() 来访问 R 中的对象属性

r - 在颜色空间中的scale_fill/color中粘贴名称不能循环工作

r - Log10 x 轴在顶部和 y 轴 geom_line

r - ggplot2 堆叠条形图,将 NA 放在顶部