0

I have a dataset with Year and Month number that I am trying to plot against another variable, in this case "MEI". Here is a small sample of the data:

Year,   Month,  MEI,
1983,   5,  2.556,
1983,   6,  2.167,
1983,   7,  1.741,
1983,   8,  1.13,
1983,   9,  0.428,
1983,   10, 0.002,
1983,   11, -0.176,
1983,   12, -0.176,
1984,   1,  -0.339,
1984,   2,  -0.565,

Is there a way to make the x axis continuous over the years (Months-11,12,1,2). I tried:

plot(weather$Month, weather$MEI, main = 'Regression for MEI Each Month Over 16.5 Years', 
xlab = 'Month (From May 1983 to December 2008', ylab = 'MEI')

and I got this scatter plot. enter image description here

Cettt
  • 11,460
  • 7
  • 35
  • 58
Defqon
  • 117
  • 11
  • 1
    Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including example data in a plain text format - for example the output from `dput(yourdata)`. We cannot copy/paste data from images. – neilfws Jan 31 '21 at 21:51
  • 1
    @neilfws It has been updated. I apologize for the poor question presentation. – Defqon Jan 31 '21 at 21:55
  • There's no strong indication that the x-axis is *not* continuous; the fact that the points are lined up like that seems likely due to the integer nature of the second column (I'm inferring `Month`). (So technically, yes, as integers it is discrete and not continuous ... but that's a property of the data, not something you can just make happen.) – r2evans Jan 31 '21 at 21:55
  • Are you trying to change the axis labels so that it shows all months, not just the even numbers? Or are you hoping for lines within a year to indicate trends? – r2evans Jan 31 '21 at 21:57
  • @r2evans Maybe continuous is the wrong word. I am wondering if there is a way to have the months of different years not on the same x value. For example, I would like each value of the x axis to be in order (November 1985, December 1985, January 1986, February 1986, etc.) – Defqon Jan 31 '21 at 21:58
  • @r2evans I am trying to see if there is a trend over the years. My full dataset is from 1985-2008 – Defqon Jan 31 '21 at 21:59

2 Answers2

1

One solution is to generate dates from the year/month values by adding "01" as the day. You can do that using dplyr::mutate. If you use ggplot2 for plotting, it will make formatting a date x-axis easier too.

library(dplyr)
library(ggplot2)

weather %>% 
  mutate(Date = as.Date(paste(Year, Month, "01", sep = "-"))) %>% 
  ggplot(aes(Date, MEI)) + 
  geom_point() + 
  scale_x_date(date_labels = "%b %Y")

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63
1

You could convert the months and the year to a date and then plot the date against MEI:

weather$date <- as.Date(paste0(weather$Year, "-", weather$Month, "-01"))

plot(weather$date, weather$MEI, main = 'Regression for MEI Each Month Over 16.5 Years', 
    xlab = 'Month (From May 1983 to December 2008', ylab = 'MEI')
Cettt
  • 11,460
  • 7
  • 35
  • 58