0

In R I'm want to plot a time-series using the base plotting system and I already have a list of data that is sorted by time. I'm trying to figure out how to get the the x axis to show the days of the week("Monday, "Tuesday", etc.) instead of showing integer values.

When I call something like

 plot(ts(my_values, frequency = 365, start = c(2000, 1)))

For instance I get the x-axis starting at 2000 and having the last notch say 2008. I tried passing Strings for the start attribute, but I get the error:

 Error in start[2L] - 1 : non-numeric argument to binary operator

So I'm guessing I have to provide a numeric value to start. Any suggestions on how to get this done?

stormyone
  • 11
  • 2

2 Answers2

0

Please checkout this question on StackOverflow: Replace X-axis integer values with own values You would probably do something like:

 plot(ts(my_values, frequency = 365, start = c(2001, 1)), xaxt = "n")
 axis(1, at=seq(2001,2008,1), labels = c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))

I understand that your sequence starts at year 2000, but there are only seven days in the week :O

Community
  • 1
  • 1
small_data88
  • 380
  • 1
  • 10
0

Mark, this may be what you are looking for:

# creating data frame
set.seed(123)
df <- data.frame(date= seq.Date(as.Date('2015-06-01'), by='day',length.out = 10),value=sample(1:10,10,replace = TRUE))
df

> df
         date value
1  2015-06-01     3
2  2015-06-02     8
3  2015-06-03     5
4  2015-06-04     9
5  2015-06-05    10
6  2015-06-06     1
7  2015-06-07     6
8  2015-06-08     9
9  2015-06-09     6
10 2015-06-10     5

To plot x-axis label as “ Monday, Tueday,...” use “%A” in the format string, for abbreviated labels “ Mon, Tue, …” use “%a” like so:

plot(value ~ date, df, xaxt = "n", type = "l")
axis(1, df$date, format(df$date, "%a"), cex.axis = .7)
hvollmeier
  • 2,956
  • 1
  • 12
  • 17