2

I have a dataframe in R where:

Date        MeanVal

2002-01     37.70722
2002-02     43.50683
2002-03     45.31268
2002-04     14.96000
2002-05     29.95932
2002-09     52.95333
2002-10     12.15917
2002-12     53.55144
2003-03     41.15083
2003-04     21.26365
2003-05     33.14714
2003-07     66.55667
 .
 .
2011-12     40.00518

And when I plot a time series using ggplot with:

ggplot(mean_data, aes(Date, MeanVal, group =1)) + geom_line()+xlab("") 
+ ylab("Mean Value")

I am getting:

enter image description here

but as you can see, the x axis scale is not very neat at all. Is there any way I could just scale it by year (2002,2003,2004..2011)?

Cettt
  • 11,460
  • 7
  • 35
  • 58
Maxxx
  • 3,688
  • 6
  • 28
  • 55
  • 1
    It's likely your `Date` variable is a character class here given its format. I'd suggest converting it to a Date class and then plotting. – zack Apr 16 '19 at 14:46
  • @zack I tried df$Date <- as.Date(df$Date, format="%Y-%m"), it becomes once i do so. – Maxxx Apr 16 '19 at 14:58
  • this should work: `df$Date <- lubridate::ymd(paste0(df$Date, "-01"))`, you'll need to have installed the `lubridate` package at some point. – zack Apr 16 '19 at 15:09
  • Maybe a dupe of this: https://stackoverflow.com/q/11547414/5325862 – camille Apr 16 '19 at 15:15
  • A reproducible data set would be excellent in this case. – Odysseus210 Apr 16 '19 at 18:14

2 Answers2

2

Let's use lubridate's parse_date_time() to convert your Date to a date class:

library(tidyverse)
library(lubridate)

mean_data %>% 
  mutate(Date = parse_date_time(as.character(Date), "Y-m")) %>%
  ggplot(aes(Date, MeanVal)) +
  geom_line()

Similarly, we can convert to an xts and use autoplot():

library(timetk)

mean_data %>% 
  mutate(Date = parse_date_time(as.character(Date), "Y-m")) %>%
  tk_xts(silent = T) %>% 
  autoplot() 

This achieves the plot above as well.

tomasu
  • 1,388
  • 9
  • 11
0
library(dplyr)

mean_data %>%
  mutate(Date = as.integer(gsub('-.*', '', Date)) %>% 
  #use the mutate function in dplyr to remove the month and cast the 
  #remaining year value as an integer
  ggplot(aes(Date, MeanVal, group = 1)) + geom_line() + xlab("") 
    + ylab("Mean Value")
Odysseus210
  • 468
  • 3
  • 9
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Apr 16 '19 at 16:13