Plotting weekly data with basic R plot() function, but want labels to be evenly spaced and non-repeating month-years?

,

I am trying to create a simple scatter plot, so weeks on the x-axis and "cases" on the y with each indicated by a dot and note I can only use base R's plot(), no ggplot.

I have data with 156 weeks (about 3 years), the weeks are anchored to March 19th 2020 date (going 7 days back and forward to create every week interval). Because of this many weeks start in the same month or include the boundary of two different months within their interval.

I'm trying to get only the 36 month-years on the x-axis labels, but it seems like there's no way to decouple the 156 data points (which I keep track of using the week start day) from the x-axis labels, so I either end up with repeat month-years or unevenly spaced tick marks. Is there any way to do this using basic R's plot() function? I cannot use ggplot. I think maybe what I need is some sort of base plot() version of ggplot's scale_x_date()?

I've tried converting the text dates to actual Date format with as.Date and then plotting that directly, but that still results in repeat month on the x-axis label...

Here's a toy example, trying to graph weeks x cases, but want the labels to be month-years rather than weeks:

    Lines <- "Week_Interval        Cases     Week_Start

    19-01-03-19-01-09   696537   2019-01-03

    19-01-03-19-01-09   718748   2019-01-10

    19-01-17-19-01-23   799355   2019-01-17

    19-01-24-19-01-30   805800  2019-01-24

    19-01-31-19-02-06   701262  2019-01-31

    19-02-07-19-02-13   531579  2019-02-07

    19-02-14-19-02-20   690068  2019-02-14

    19-02-21-19-02-27   756947  2019-02-21

    19-02-28-19-03-06   718757  2019-02-28

     9-03-07-19-03-13  701768    2019-03-07

    19-03-14-19-03-20  820113   2019-03-14

    19-03-21-19-03-27  645259   2019-03-21"

  
    exampledata <- read.table(textConnection(Lines), header = TRUE)


    exampledata$Date <- as.Date(exampledata$Week_Start)

    # Plot outcome variable versus time

    plot(exampledata$Date,exampledata$Cases,

    main="Weeks by Cases",

    ylab="Cases",

    ylim=c(500000,900000),

    xlab="",

    las=2,

    col="red",

    xaxt="n")

   

    # Add x-axis year labels

    axis(1, exampledata$Date, format(exampledata$Date, "%b, %Y"))

   

    # Add in the points for the figure

    points(exampledata$Date,exampledata$Cases,

    col="red",

    pch=20)

I've answered this question on https://www.reddit.com/r/rprogramming/comments/1ebx0wx/plotting_weekly_data_with_basic_r_plot_function/ as follows:

you can create a a character vector for the text of the x axis, for example;

exampledata$label <- format.Date(exampledata$Week_Start, format = "%b\n%y")

and then assign those labels using the axis function

# find  the first rows of exampledata for each label
fr <- exampledata[!duplicated(exampledata$label), ]
# find the row numbers of the first rows
frn <- match(fr$Week_Start, exampledata$Week_Start)
axis(1, at = frn, labels = unique(exampledata$label))
1 Like

This was crossposted; the accepted answer on stackoverflow is here :