-2

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.

USAL
  • 53
  • 1
  • 10

1 Answers1

0

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

Community
  • 1
  • 1
Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • Thank you Marc for your reply. I tried this new code but the graph still shows the variations at each point. rdate contains date and time. it returns date which is labeled at x-axis. do you have any other solution? – USAL Jun 23 '15 at 12:48
  • @Shamaila - maybe you should include a short example of your code (and data), and then I can better assist you. e.g. insert a smaller version of the data structure result from `dput(temp[1:20,])` – Marc in the box Jun 23 '15 at 14:12
  • @ Marc This is a small subset of the dataset. Sensor.ID Building.ID Date.Time Reading 1 1 155 25/09/2014 16:28:58 23.77 2 1 155 25/09/2014 16:29:58 24.12 3 1 155 25/09/2014 16:30:58 24.18 4 1 155 25/09/2014 16:31:58 24.04 5 7 155 25/09/2014 16:32:11 24.27 6 1 155 25/09/2014 16:32:58 23.88 7 7 155 25/09/2014 16:33:11 24.92 8 1 155 25/09/2014 16:33:58 23.65 9 7 155 25/09/2014 16:34:11 25.16 10 1 155 25/09/2014 16:34:58 23.41 and code is the same as I have pasted in my question. Any help will be much appreciated. – USAL Jun 23 '15 at 14:38
  • @Shamaila - I think the edited answer should help you now – Marc in the box Jun 24 '15 at 05:40