-1

I have dates as 'Dec-17','Jan-18','Feb-18','Mar-18','Apr-18','May-18','Jun-18','Jul-18','Aug-18' When I plot I need to preserve this in x axis. But x axis is getting reordered. I tried factors method, then also I get the same :

> data_$Dates
[1] Apr-18 Aug-18 Dec-17 Feb-18 Jan-18 Jul-18 Jun-18 Mar-18 May-18
9 Levels: Apr-18 Aug-18 Dec-17 Feb-18 Jan-18 Jul-18 Jun-18 ... May-18

But, I need to follow the same formatting starting from Dec-17 and not Apr-18 My code :

data_$Dates <- factor(data_$Dates, levels = data_$Dates[order(data_$`Expected`)])
data_$Dates
ggplot(data=data_, aes(x=Dates, y=`Expected`, group=1)) +
  geom_line()+
  geom_point()
qwww
  • 1,313
  • 4
  • 22
  • 48
  • 1
    Possible duplicate of [Change the order of a discrete x scale](https://stackoverflow.com/questions/3253641/change-the-order-of-a-discrete-x-scale) – Niek Oct 30 '18 at 07:36
  • 3
    easiest if you make your x-vlues actual dates, using `as.Date("2018-01-01", etc...)`, and then format your date-labels in `scale_x_date(date_breaks = "1 month, date_labels = "%B")`.. some sample data would be nice... – Wimpel Oct 30 '18 at 07:41
  • @WimpelI need them to be shown as DEC-17,Jan-18 like that. And this can become dynamic in future,,so that I cant hardcode it.. I need to show that specific text itself – qwww Oct 30 '18 at 08:56
  • @qwww Isn't that what Wimple's suggestion gives you? Correctly sorted date values shown in a desired format? Pls include some sample data if you want more specific assistance. – Z.Lin Oct 30 '18 at 08:56

2 Answers2

1

You're reordering alphabetically. You'll probably want to either convert your data to a data type that is interpreted as such (as suggested by @Wimpel in his/her comment). Alternatively, as you only have 9 unique values, you may want to reorder manually:

date_$Dates <- factor(date_$Dates, levels = c("Dec-17", "Jan-18", "Feb-18"))

(which you'll have to expand to include the six months I have omitted).

janverkade
  • 124
  • 1
  • 8
  • I need them to be shown as DEC-17,Jan-18 like that. And this can become dynamic in future,,so that I cant hardcode it.. I need to show that specific text itself – qwww Oct 30 '18 at 08:56
1

Let's generate some sample data first:

set.seed(123)
data_ <- data.frame(Expected = sample(1:10, 9), Dates = c('Dec-17','Jan-18',
    'Feb-18','Mar-18', 'Apr-18', 'May-18','Jun-18', 'Jul-18', 'Aug-18'),
    stringsAsFactors = FALSE)

As @Wimpel told, the most natural way is to transform your data-column into a Data-type column. Here you may find a comprehensive discussion on possible ways for that. One of the most convenient solutions is using of the lubridate package:

library(lubridate)
data_$Dates_data <-  as.Date(parse_date_time(data_$Dates, "m-y"))

Now you have a date-formatted column to be used as x-data, and you may adjust the format of x labels according to your preferences (again, as @Wimpel suggested):

pl_1 <- ggplot(data = data_, aes(x = Dates_data, y = Expected, 
    group = 1)) +
  geom_line() +
  geom_point() +
  scale_x_date(date_labels = "%b-%y", date_breaks = "1 month")
plot(pl_1)
Ekatef
  • 1,061
  • 1
  • 9
  • 12