0

This is in R ver3.4.2 programming.I am about to plot a certain variable of a dataframe, say Y, against the weekdays of the Dates. I have been through some references but none so far worked. I have tried the following: (the dataframe has Date, Time and Y)

library(dplyr)
library(lubridate)
library(datasets)
df$Date<- as.Date(df$Date, "%d/%m/%Y")
df$Time<- strptime(df$Time, "%H:%M:%S")
df$Time<- as.POSIXct(df$Time)
#select data for certain dates
selected_data<-with(df, df[(Date >= "2017-01-12" & Date <= "2017-01-13"),])
Y<- as.numeric(as.character(selected_data$Y))
#these are my trials in trying to plot the weekdays
#trial 1
wkdays <- weekdays(selected_data$Date)
#trial 2
dat <- format(selected_data$Date,"%Y-%m-%d")

pl<-plot(dat,Y, type="l" , ylab = "Y", xlab=" ")
#pl<-plot(wkdays, Y, type="l" ,xaxt="n", ylab = "Y", xlab=" ")
#pl<- plot(Y, type="l", ylab = "Y", xlab=" ")
axis(1,at=as.Date.numeric(selected_data$Date), labels=format(weekday))
#axis.Date(1, at=seq(weekday), "days",labels = TRUE)
#axis.Date(1, at=seq(min(wkdays),max(wkdays), by = "%d"), "days",labels = TRUE,tcl = -0.2)
axis(2, at= seq(0, max(Y), by = 2))

The output should be a line plot of Y data vs Index with ticks labeled with the weekdays of the Dates (Mon, Tue, etc) with a certain tick intervals.

If anyone has a link with the answer (maybe I have diff keywords), please help and share it below. Thank you!

Here's a sample dataset:

Date;Time;Y;
2017-01-12;2018-02-27 00:00:00;"0.715";
2017-01-12;2018-02-27 00:01:00;"0.438";
2017-01-12;2018-02-27 00:02:00;"0.734";
2017-01-12;2018-02-27 00:03:00;"0.646";
2017-01-12;2018-02-27 13:19:00;"1.268";
2017-01-13;2018-02-27 13:20:00;"0.108";
2017-01-13;2018-02-27 13:21:00;"0.961";
2017-01-13;2018-02-27 13:22:00;"0.570";
2017-01-13;2018-02-27 13:23:00;"0.192";
2017-01-13;2018-02-27 13:24:00;"1.280";
athrun
  • 29
  • 9
  • Please provide a reproducible example so we can test your code. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Djork Feb 27 '18 at 05:01

1 Answers1

0

Here is a solution using scale_x_date(date_breaks = "day", date_labels = "%a") from the ggplot2 package. See more here and here.

Since you didn't provide your data, I generated a random one to work with.

library(ggplot2)

set.seed(2017)
date <- seq(from = as.Date("2017-12-01"), to = as.Date("2017-12-13"),
            by = "days")
value <- runif(length(date), 1, 1000)
dat <- data.frame(date, value)

ggplot(dat, aes(x = date, y = value)) + 
  geom_point() + 
  geom_line() +
  scale_x_date(date_breaks = "day", date_labels = "%a") +
  theme_bw()

To include the day & month:

ggplot(dat, aes(x = date, y = value)) + 
  geom_point() + 
  geom_line() +
  scale_x_date(date_breaks = "day", date_labels = "%a\n%b %d") +
  theme_bw()

Created on 2018-02-26 by the reprex package (v0.2.0).

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Thank you for your help. the dates are "2017-01-12" & "2017-01-13" only, i have added a sample dataset in the post. I have tried your code but I used my dates but it returned an error --> "Error: Aesthetics must be either length 1 or the same as the data (2880): x, y".. – athrun Feb 27 '18 at 17:51