【发布时间】:2023-07-20 08:01:02
【问题描述】:
我需要绘制平均每月温度,并在 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 个月,手段会有所不同,但实现相同的目标。
【问题讨论】:
-
谢谢@Rui Barradas
-
我注意到一个小问题,您需要
%m/%d/%Y %H:%M转换日期/时间,大写“Y”,因为年份是 4 位数而不是 2 -
我很抱歉,正如我所说的我在这方面很新,它像这样导入到 R... 所以小写的 y 是正确的,其他数据来自 excel。我应该更清楚。日期...时间 温度..C.盐度.psu。传导.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 14.35 /13/17 4:00 14.68 24.35 30.72 6 1/13/17 5:00 14.65 24.35 30.70
标签: r date ggplot2 confidence-interval