0
votes

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

2
Please make this question reproducible by including example data in a plain text format - for example the output from dput(yourdata). We cannot copy/paste data from images. - neilfws
@neilfws It has been updated. I apologize for the poor question presentation. - Defqon
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
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
@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

2 Answers

1
votes

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

1
votes

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')