Extract Month name and year and the sort them to be used as x.axis in R

Viewed 121

I have tried a number of methods realise that but in vain . I have data in terms of a date (DD.MM.YY) and am trying to get the following : Name of the Month-YYYY and then sort them to use them as x.axis:

ID    Date         Month_Yr
1     02.08.2022   August-2022
2     03.11.2022   November-2022
3     07.06.2021   Jun-2021
1 Answers

Sounds a bit like a XY-problem.

I suggest to do the labelling using ggplot's scale_x_date()

#sample data
mydata <- data.frame( date = seq(as.Date("2022-01-01", format = "%Y-%m-%d"), 
                                 as.Date("2022-12-31", format = "%Y-%m-%d"), by = 1),
                      value = 1:365)


#plot with data-axis an labels
library(ggplot2)
ggplot(data = mydata, aes(x = date, y = value)) +
  geom_line() +
  scale_x_date(date_breaks = "1 month", date_labels =  "%b-%Y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

enter image description here

Related