0

The x axes of my graph are defined as follow: Dmmggyyyy, where D is a char. Due to the fact that I´m working on yearly basis when I print the graph the x axes texts are unreadable. Is possible to use a regex and group datas from D01012015 to D02012015 etch. and call them January, February... The source code of my plot is:

df <- melt(data)    
pl <- ggplot(df, aes(time, price,group=factor(xxx))) + 
  geom_line(aes(color=factor(xxx)))+
  theme(axis.text.x element_blank())
Jaap
  • 81,064
  • 34
  • 182
  • 193
Jua'
  • 51
  • 3
  • 9
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap May 04 '16 at 18:03
  • You can convert those strings to a date-format with for example `strptime('D01012015', format = 'D%d%m%Y')`. If you want just the month, you can use: `months(strptime('D01012015', format = 'D%d%m%Y'))` – Jaap May 04 '16 at 18:08

2 Answers2

1

You can convert those strings to a date-format with for example strptime('D01012015', format = 'D%d%m%Y'):

[1] "2015-01-01 CET"

If you want just the month, you can use: months(strptime('D01012015', format = 'D%d%m%Y')):

[1] "januari"
Jaap
  • 81,064
  • 34
  • 182
  • 193
0

You can strip the 'D' and then convert to date. I like using lubridate.

install.packages('lubridate')
library(lubridate)

df$x <- gsub("D", "", df$x)
df$x <- mdy(df$x)

From there you can get the months,

df$months <- month(df$x)
Raphael K
  • 2,265
  • 1
  • 16
  • 23