R中的折线图不显示一条线,而是显示每个点的变化
Line graph in R is not showing a line but variations at each point
我正在尝试绘制温度时间序列的折线图。它不是制作一条精确的线,而是显示每个点的变化。
我已经为这个折线图写了这段代码
temp<-read.csv("E:/Salford Work/Data Mining/Data/Tensor houses/8 John boste court/temReadings (5).csv");
rdate <- as.Date(temp$Date.Time, "%d/%m/%Y %H:%M");
plot(temp$Reading~rdate, type="l", col="blue", axes=F);
axis(1,rdate,format(rdate,"%d-%m-%y"));
read<-temp[,4];
axis(2,read);
谁能帮我画一条简单的线。
这通常是由于具有分类的 x 轴变量。我不确定你是否属于这种情况,因为你似乎已经转换为 as.Date
。不过,当不在绘图函数中调用数据框时,我会避免使用公式 (~
) 表示法:
plot(rdate, temp$Reading, type="l", col="blue", axes=F)
无论哪种方式,请确保 class(rdate)
returns "Date"
.
编辑:
问题是使用 as.Date
时,小时、分钟和秒将从您的数据中删除,并且您的大部分日期都在同一天 - 因此您无法解决问题。相反,您应该使用“POSIXlt”或“POSIXct”格式并使用strptime
函数进行转换:
temp <- structure(list(Sensor.ID = c(1L, 1L, 1L, 1L, 7L, 1L, 7L, 1L,
7L, 1L), Building.ID = c(155L, 155L, 155L, 155L, 155L, 155L,
155L, 155L, 155L, 155L), Date.Time = structure(1:10, .Label = c("25/09/2014 16:28:58",
"25/09/2014 16:29:58", "25/09/2014 16:30:58", "25/09/2014 16:31:58",
"25/09/2014 16:32:11", "25/09/2014 16:32:58", "25/09/2014 16:33:11",
"25/09/2014 16:33:58", "25/09/2014 16:34:11", "25/09/2014 16:34:58"
), class = "factor"), Reading = c(23.77, 24.12, 24.18, 24.04,
24.27, 23.88, 24.92, 23.65, 25.16, 23.41)), .Names = c("Sensor.ID",
"Building.ID", "Date.Time", "Reading"), class = "data.frame", row.names = c(NA,
10L))
rdate <- strptime(temp$Date.Time, "%d/%m/%Y %H:%M")
class(rdate)
plot(rdate, temp$Reading, type="l", col="blue")
axis(1,rdate,format(rdate,"%d-%m-%y"))
我正在尝试绘制温度时间序列的折线图。它不是制作一条精确的线,而是显示每个点的变化。
我已经为这个折线图写了这段代码
temp<-read.csv("E:/Salford Work/Data Mining/Data/Tensor houses/8 John boste court/temReadings (5).csv");
rdate <- as.Date(temp$Date.Time, "%d/%m/%Y %H:%M");
plot(temp$Reading~rdate, type="l", col="blue", axes=F);
axis(1,rdate,format(rdate,"%d-%m-%y"));
read<-temp[,4];
axis(2,read);
谁能帮我画一条简单的线。
这通常是由于具有分类的 x 轴变量。我不确定你是否属于这种情况,因为你似乎已经转换为 as.Date
。不过,当不在绘图函数中调用数据框时,我会避免使用公式 (~
) 表示法:
plot(rdate, temp$Reading, type="l", col="blue", axes=F)
无论哪种方式,请确保 class(rdate)
returns "Date"
.
编辑:
问题是使用 as.Date
时,小时、分钟和秒将从您的数据中删除,并且您的大部分日期都在同一天 - 因此您无法解决问题。相反,您应该使用“POSIXlt”或“POSIXct”格式并使用strptime
函数进行转换:
temp <- structure(list(Sensor.ID = c(1L, 1L, 1L, 1L, 7L, 1L, 7L, 1L,
7L, 1L), Building.ID = c(155L, 155L, 155L, 155L, 155L, 155L,
155L, 155L, 155L, 155L), Date.Time = structure(1:10, .Label = c("25/09/2014 16:28:58",
"25/09/2014 16:29:58", "25/09/2014 16:30:58", "25/09/2014 16:31:58",
"25/09/2014 16:32:11", "25/09/2014 16:32:58", "25/09/2014 16:33:11",
"25/09/2014 16:33:58", "25/09/2014 16:34:11", "25/09/2014 16:34:58"
), class = "factor"), Reading = c(23.77, 24.12, 24.18, 24.04,
24.27, 23.88, 24.92, 23.65, 25.16, 23.41)), .Names = c("Sensor.ID",
"Building.ID", "Date.Time", "Reading"), class = "data.frame", row.names = c(NA,
10L))
rdate <- strptime(temp$Date.Time, "%d/%m/%Y %H:%M")
class(rdate)
plot(rdate, temp$Reading, type="l", col="blue")
axis(1,rdate,format(rdate,"%d-%m-%y"))