Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R中的日期和时间绘制连续数据点_R_Date_Ggplot2_Timestamp - Fatal编程技术网

使用R中的日期和时间绘制连续数据点

使用R中的日期和时间绘制连续数据点,r,date,ggplot2,timestamp,R,Date,Ggplot2,Timestamp,我是RStudio的新手,所以我的编码是初级的 我有一个包含六(6)列的数据集:date5m、time5m、T5m、date28m、time28m、T28m。数据集是两个深度(5m和28m)的温度数据,带有相关日期和时间戳。我生成的图表似乎按天放置所有数据,而不是按收集时间连续显示。任何协助都将不胜感激 library(ggplot2) library(scales) library(dplyr) Aberdeen <- read.csv(file.choose(), header = T

我是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"))

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

“Aberdeen”是
read.csv
语句中原始数据帧的名称(假设与发布的示例数据匹配)。有关更多详细信息,请参见代码注释:

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"))
library(tidyr)
图书馆(dplyr)
图书馆(stringr)
#重命名这些列以在字母和第一个数字之间添加“u”分隔符
#这是为了使分离和枢轴更容易。
#请参见tidyr pivot渐晕图“每行多个观测值”
姓名(阿伯丁)%str_替换(“(\\D)(\\D)”,“\\1\\2”)
#添加用于跟踪目的的行号
#将日期和时间列合并为一列
#长形
dflong%变异(rowid=row_number())%>%
联合体(“日期时间5m”,c(日期5m,时间5m))%>%
联合(“日期时间28m”,c(日期28m,时间28m))%>%
pivot_longer(cols=-rowid,names_to=c(“.value”,“depth”),names_sep=“”)
#将datetime列从字符转换为datetime项目:

dflong$datetime由于行中的日期和时间列不一致,因此结果比预期的更复杂。 我必须操纵列名,以便在名称中提供一致的分隔符。我还将日期和时间列合并到一个datetime对象中,以便正确绘制。
一旦原始数据帧从原始宽格式转换为长格式,
ggplot
调用就简化了

“Aberdeen”是
read.csv
语句中原始数据帧的名称(假设与发布的示例数据匹配)。有关更多详细信息,请参见代码注释:

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"))
library(tidyr)
图书馆(dplyr)
图书馆(stringr)
#重命名这些列以在字母和第一个数字之间添加“u”分隔符
#这是为了使分离和枢轴更容易。
#请参见tidyr pivot渐晕图“每行多个观测值”
姓名(阿伯丁)%str_替换(“(\\D)(\\D)”,“\\1\\2”)
#添加用于跟踪目的的行号
#将日期和时间列合并为一列
#长形
dflong%变异(rowid=row_number())%>%
联合体(“日期时间5m”,c(日期5m,时间5m))%>%
联合(“日期时间28m”,c(日期28m,时间28m))%>%
pivot_longer(cols=-rowid,names_to=c(“.value”,“depth”),names_sep=“”)
#将datetime列从字符转换为datetime项目:

dflong$DateTime请分享一点可复制的数据。如果您向我们提供
dput(droplevels(Aberdeen[1:10,]))
的结果,它将为我们提供前10行数据的复制/粘贴版本。我可以给您一个提示,这与您的问题没有直接关系。我看到您已经阅读了csv,并且正在使用名为ï..date1的变量。这些是使用BOM表标题保存的文件,可能是由SQL Server或其他软件保存的。您可以通过在read.csv()中包含参数fileEnconding=“UFT-8-BOM”来处理此问题。似乎您可以使用geom_line()而不是geom_point()来为其提供连续的外观。我们也期待dput()的出现,以便提供进一步的建议。您需要使用
collect
pivot\u longer
函数将宽数据帧转换为长格式。请看这个问题:添加了一个样本数据集@GregorThomas请共享一点可复制的数据。如果您向我们提供
dput(droplevels(Aberdeen[1:10,]))
的结果,它将为我们提供前10行数据的复制/粘贴版本。我可以给您一个提示,这与您的问题没有直接关系。我看到您已经阅读了csv,并且正在使用名为ï..date1的变量。这些是使用BOM表标题保存的文件,可能是由SQL Server或其他软件保存的。您可以通过在read.csv()中包含参数fileEnconding=“UFT-8-BOM”来处理此问题。似乎您可以使用geom_line()而不是geom_point()来为其提供连续的外观。我们也期待dput()的出现,以便提供进一步的建议。您需要使用
collect
pivot\u longer
函数将宽数据帧转换为长格式。查看此问题:添加了一个样本数据集@GregorThomas