2

I am plotting 15 months of data and by default R is only plotting one x-axis tick mark, "2016". Ideally, I would have tick marks at intervals of every three, with labels such as "Sept" or "9/1/2015".

So far, I have succeeded in getting R to plot more tick marks, but the x-axis values don't display dates. I am not even sure if they correspond to the first of each month at three-month intervals (Sept, Dec, etc.), which would be my preference.

I have already various things, including:

#1
axis(1, at = seq(as.Date("2015-09-11"), as.Date("2016-12-11"), by = "month"))

#2
xaxp=c(as.Date("2015-09-11"), as.Date("2016-12-11"), 15)

#3
axis.Date(1, temperature_subset_maximum_EMI_2015_DATE_CONVERTED, at=seq(as.Date(temperature_subset_maximum_EMI_2015_DATE_CONVERTED), by="months", labels = TRUE, origin="1970-01-01")

#4
z<-cbind("2015-09-01", "2015-10-01", "2015-11-01", "2015-12-01", "2016-01-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01", "2016-07-01", "2016-08-01", "2016-09-01", "2016-10-01", "2016-11-01","2016-12-01"
z<-as.Date(z, format="%Y-%m-%d")
class(z)
# [1] "Date"

#5
axis(side=1, at=c(1:15), labels=c("September","October","November","December","January","February","March","April","May","June","July", "August","September","October","November","December")

#6
axis.POSIXct(1, temperature_subset_maximum_EMI_2015_DATE_CONVERTED, format(seq.Date(as.Date("2015-09-01"), by = "month", len = 5)), labels = TRUE)

My most successful code so far is:

plot(x, y, xlim=c(as.Date("2015-09-11"),as.Date("2016-12-01")), ylim=c(0,30),xaxt="n", xlab="Date")
axis(1, at = seq(as.Date("2015-09-11"), as.Date("2016-12-31"), by = 93))

As I mentioned, I have been able to get more tick marks to show up on the x-axis, but I don't know if they denote the first the month (in one or three-month intervals), and the dates or names of months that they denote do not show up on the x-axis (instead 5 digit numbers show up). Currently, there are no error messages when I run the code. Below is the output: Graph with 2015-2016 data, but dates won't show up on x-axis

  • Suggested reading: https://stackoverflow.com/editing-help#syntax-highlighting, as a more-readable question (esp code-blocks) can make it much easier to read. – r2evans Aug 31 '19 at 21:26
  • Also, please be careful when pasting code: you were missing a quote (`"October`), which would mean your code could never run; while I suspect that is not missing in your code, it is distracting when trying to help you and figure out what is going on. – r2evans Aug 31 '19 at 21:28
  • 1
    @r2evans, thanks for those suggestions. – user12004190 Sep 01 '19 at 00:09

1 Answers1

-1

First: See how to provide a minimal reproducible example, to help us answering your question.

If I get you right, you want axis breaks with a three month interval. If you have dates (no date strings) in your data frame, this can be done using the scale_x_date-command.

# monthly interval
scale_x_date(name="Month", date_breaks="1 month", minor_breaks=NULL, date_labels="%b")
# 3-month interval
scale_x_date(name="3 Months", date_breaks="3 month", minor_breaks=NULL, date_labels="%b")

The label %b means abbreviated month names.

To convert date stings into dates you could use the conversion functions in the lubridate-package, e.g ymd(date_string).

chgrl
  • 106
  • 5