R - Plot value vs character date -
i want plot (in single plot) 2 data frame columns composed of variables "my.data$v2" (y axis) vs date "my.data$v3" (x axis) character string. i've tried using base plot , ggplot approaches, couldn't work.
plot(my.data$v2,my.data$v3,xaxt="n",ylab="volum") #don't plot x axis axis.posixct(1, at=seq(my.data$v3[1], my.data$v3[2], by="month"), format="%b") #label x axis months require(ggplot2) theme_set(theme_bw()) # change theme preference ggplot(aes(x = my.data$v3, y = my.data$v2), data = my.data) + geom_point()
here can find dataset. load 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")
your dataset comprises of nothing character strings. need 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, should refer variables name (without $
operator) inside aes()
. see here great explanation on this:
ggplot(aes(x = v3, y = v2), data = my.data) + geom_point()
Comments
Post a Comment