Line graph in R is not showing a line but variations at each point

83 views Asked by At

I am trying to plot a line graph for temperature time-series. Instead of making an exact line it shows variations at each point.

I have written this code for this line graph

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);

Can anyone please help me to draw a simple line.

1

There are 1 answers

4
Marc in the box On BEST ANSWER

This usually results from having an x-axis variable that is categorical. I'm not sure if this is the case for you since you seem to have converted to as.Date. Nevertheless, I would avoid the formula (~) notation when not calling a dataframe in your plot function:

plot(rdate, temp$Reading, type="l", col="blue", axes=F)

Either way, make sure that class(rdate) returns "Date".

Edit:

The problem is that the hours, minutes, and seconds are getting removed from your data when using as.Date, and most of your dates are on the same day - Thus you lose resolution. Instead, you should use "POSIXlt" or "POSIXct" format and use the strptimefunction for the conversion:

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

enter image description here