【问题标题】:Graphing continuous data points using date and time in R在 R 中使用日期和时间绘制连续数据点
【发布时间】:2020-07-03 06:01:57
【问题描述】:

我对 RStudio 很陌生,所以我的编码很初级。

我有一个包含六 (6) 列的数据集:date5m、time5m、T5m、date28m、time28m、T28m。该数据集是两个深度(5m 和 28m)的温度数据,带有相关的日期和时间戳。我生成的图表似乎按天放置所有数据,而不是按收集时间连续显示。任何援助将不胜感激。

library(ggplot2)
library(scales)
library(dplyr)
Aberdeen <- read.csv(file.choose(), header = TRUE)
head(Aberdeen)
Aberdeen$ï..date5m = as.Date(Aberdeen$ï..date5m, format = "%Y-%m-%d")
Aberdeen$date28m = as.Date(Aberdeen$date28m, format = "%Y-%m-%d")
ggplot() + geom_point(data = Aberdeen, aes(x = ï..date5m, y = T5m), 
    colour = "darkgreen", size=0.25, na.rm=TRUE) + 
    geom_point(data = Aberdeen, aes(x = date28m, y = T28m), colour = "forestgreen", size=0.25, na.rm=TRUE) + 
    labs(x = "Date", y = "Temperature (\u00B0C)") + 
    ggtitle("Aberdeen") + 
    theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + 
     scale_x_date(date_breaks = "month", labels=date_format("%b-%Y"))

我希望图表使用日期和时间戳以连续方式显示数据,如下所示:

这是我的数据集的前 10 行:

structure(list(date5m = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "2018-06-01", class = "factor"), time5m = 
structure(1:10, .Label = c("14:40:30", 
"14:42:34", "14:44:39", "14:46:40", "14:48:43", "14:50:46", "14:52:51", 
"14:54:56", "14:56:59", "14:59:03"), class = "factor"), T5m = c(9.1, 
9.02, 9, 9.12, 9.12, 9.1, 9.06, 9.02, 8.98, 9.02), date28m = 
structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "2018-06-01", class = 
"factor"), 
time28m = structure(1:10, .Label = c("14:39:00", "14:49:00", 
"14:59:00", "15:10:00", "15:20:00", "15:30:00", "15:40:00", 
"15:50:00", "16:00:00", "16:11:00"), class = "factor"), T28m = c(1.93, 
1.93, 1.93, 1.93, 1.93, 1.93, 1.93, 1.93, 1.93, 1.91)), row.names = c(NA, 
10L), class = "data.frame")

【问题讨论】:

  • 请分享一些可重现的数据。如果您给我们dput(droplevels(Aberdeen[1:10, ])) 的结果,它将给我们前 10 行数据的复制/粘贴版本。
  • 我可以给你一个提示,它与你的问题没有直接关系。我看到您已阅读 csv 并使用名为ï..date1 的变量。这些是使用 BOM 标头保存的文件,可能由 SQL Server 或其他一些软件保存。您可以通过在 read.csv() 中包含调用参数 fileEnconding = "UFT-8-BOM" 来处理这个问题。
  • 看起来你可以使用 geom_line() 而不是 geom_point() 来给它连续的外观。我们也期待 dput() 以便我们提供进一步的建议。
  • 您需要使用gatherpivot_longer 函数将宽数据框转换为长格式。看到这个问题:stackoverflow.com/questions/45795429/…
  • 添加了一个样本数据集@GregorThomas

标签: r date ggplot2 timestamp


【解决方案1】:

这比预期的要复杂,因为日期和时间列在各行中不一致。 我必须操纵列名以在名称中提供一致的分隔符。我还将日期和时间列组合成一个日期时间对象,以便正确绘制。
将原始数据帧从原始宽格式转换为长格式后,ggplot 调用就被简化了。

“Aberdeen”是来自read.csv 语句的原始数据框的名称(假设与发布的示例数据匹配)。更多细节请查看代码 cmets:

library(tidyr)
library(dplyr)
library(stringr)

#Rename the columns to add a '_' seperator between the letter and first number
#this is needed to make the separation and the pivot easier.
# See the tidyr pivot Vignette "Multiple observations per row"
names(Aberdeen) <- names(Aberdeen) %>% str_replace( "(\\D)(\\d)", "\\1_\\2")

#Adding a rownumber for tracking purposes
#Unite the date and time columns into 1 column
#reshape to long
dflong<-Aberdeen %>% mutate(rowid=row_number()) %>%
  unite("datetime_5m",  c(date_5m,  time_5m)) %>% 
  unite("datetime_28m",  c(date_28m,  time_28m)) %>% 
  pivot_longer(cols= -rowid, names_to = c(".value", "depth"), names_sep="_") 

#convert datetime column from character to datetime oject:
dflong$datetime<-as.POSIXct(dflong$datetime, "%Y-%m-%d_%H:%M:%S", tz="")

#plot grouping and coloring by the depth
ggplot(data = dflong, aes(x = datetime, y = T, group=depth, color=depth)) + 
  geom_point() + 
  labs(x = "Date", y = "Temperature (\u00B0C)") + 
  ggtitle("Aberdeen") + 
  theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + 
  scale_x_datetime(date_breaks = "hour", labels=date_format("%b-%Y"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多