0

I want to plot (in a single plot) two data frame columns composed of variables "my.data$V2" (y axis) vs date "my.data$V3" (x axis) which is a character string. I've tried using the base plot and ggplot approaches, but I couldn't get it work.

plot(my.data$V2,my.data$V3,xaxt="n",ylab="Volum")          #don't plot the x axis
axis.POSIXct(1, at=seq(my.data$V3[1], my.data$V3[2], by="month"), format="%b") #label the x axis by months


require(ggplot2)
theme_set(theme_bw()) # Change the theme to my preference
ggplot(aes(x = my.data$V3, y = my.data$V2), data = my.data) + geom_point()

Here you can find the dataset. Load the file using load("data.Rdata")

Solved: Using

plot(strptime(my.data$V3,"%d/%m/%YT%H:%M:%S",tz="GMT"),my.data$V2, xlab="Time", ylab="Volum")
waka
  • 3,362
  • 9
  • 35
  • 54
R user
  • 131
  • 3
  • 14
  • Please add your solution in a distinct answer + do not add solved in title, we have the accepted answer to indicate that a solution worked for you. – Petter Friberg Sep 12 '17 at 09:33

1 Answers1

1

Your dataset comprises of nothing but character strings. You need to convert them first.

> str(my.data)
'data.frame':   17670 obs. of  3 variables:
 $ V1: chr  "236E01VE" "236E01VE" "236E01VE" "236E01VE" ...
 $ V2: chr  "2.571" "2.571" "2.571" "2.571" ...
 $ V3: chr  "13/06/2017T12:55:00" "13/06/2017T13:00:00" "13/06/2017T13:05:00" "13/06/2017T13:10:00" ...

my.data$V2 <- as.numeric(my.data$V2)
my.data$V3 <- as.POSIXct(my.data$V3, format = "%d/%m/%YT%H:%M:%S")

> str(my.data)
'data.frame':   17670 obs. of  3 variables:
 $ V1: chr  "236E01VE" "236E01VE" "236E01VE" "236E01VE" ...
 $ V2: chr  "2.571" "2.571" "2.571" "2.571" ...
 $ V3: POSIXct, format: "2017-06-13 12:55:00" "2017-06-13 13:00:00" "2017-06-13 13:05:00" "2017-06-13 13:10:00" ...

For ggplot, you should refer to the variables by name (without the $ operator) inside aes(). See here for a great explanation on this:

ggplot(aes(x = V3, y = V2), data = my.data) + 
  geom_point()

plot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94