0
votes

I have the following data set:

Percentage  Date    Category
0.001546278 1   A
0.002334523 2   A
0.00089928  3   A
0.00205846  4   A
0.000994036 1   B
0.000961275 2   B
6.00E-04    3   B
0.001132153 4   B
0.000773139 1   C
0.000411975 2   C
0.00039968  3   C
0.000205846 4   C

which I use to make a graph by:

ggplot(data.frame(data), aes(x = Date, y = Percentage, fill = Category)) + 
  geom_area(position = 'stack')

what I would like is to get in the x-axes a date instead of the sequence 1-4: i.e.: Jan 2001, Feb 2002, March 2002 and April 2002. Any date format will do, as long as month and year appear. Any suggestions on how to make it?

2

2 Answers

2
votes

Use yearmon() function from zoo package.

For example : Substitute :

     x = as.yearmon(as.Date("2015-12-25", %Y-%m-%d),"%y-%m")
1
votes

You could do

ggplot(data.frame(data), aes(x = as.Date("2000-12-01")+months(Date), y = Percentage, fill = Category)) + 
  geom_area(position = 'stack') + 
  scale_x_date(date_labels = "%b %Y")