0

Hi all and apologies if this came up but I could not find 100% match answer to my question...

I have a csv file named zed made of 3 cols and 366 observation: FTD_DAY, FTD_DATE, FTD imported in R. FTD_DATE looks like a date (dd/mm/yyy) but R reads it as integer (typeof(zed$FTD_DATE) displays integer in GUID).

I then geom_point the data using:

x <-ggplot(data=zed, aes(x=FTD_DATE,y=FTD)) and the x axis looks ugly since cannot fit 366 dates making it unreadable.

Questions I have:

  1. How can I order dates in x in ascending order?
  2. How can I break coordinates showing only (say) 7 coords maintaining all 366 observations?
  3. How can I align the dates in x axis vertically?

I tried scale_discrete(), scale_log() etc. but went nowhere or got somewhere in the wrong place... can someone please help and let me know what is the solution using ggplot?

Val
  • 6,585
  • 5
  • 22
  • 52
AlGrasso
  • 1
  • 3
  • Take a look at `scale_x_date`, `scales::pretty_breaks(n = 7)` & `theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1.0))` – Tung Mar 02 '18 at 14:59
  • Hi Tung, thanks for your input. I tried scale_x_date() but it gave me Error: Invalid input: date_trans works with objects of class Date only (perhaps it has to do with FTD_DATE typeof = integer?). I could not find documentation over scales::pretty_breaks.. is it an argument of scale_x_date? In any case I am trying different options but cannot get to the solution. I will keep trying but any help would be much appreciated. Thanks – AlGrasso Mar 02 '18 at 15:26
  • Try to use as.Date(FTD_DATE ,format = "%d/%m/%Y"). R has to be told this is a datetime data. – Wenlong Liu Mar 02 '18 at 15:32
  • Can you share your data using `dput()`? See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Tung Mar 02 '18 at 15:33
  • str(zed) 'data.frame': 5 obs. of 3 variables: $ FTD_DAY : Factor w/ 7 levels "FRI","MON","SAT",..: 7 1 1 7 1 $ FTD_DATE: Factor w/ 366 levels "01/01/2018","01/02/2018",..: 47 323 187 154 19 $ FTD : int 301 315 251 303 218 Summary(zed) FTD_DAY FTD_DATE FTD FRI:3 02/06/2017:1 Min. :218.0 MON:0 04/10/2017:1 1st Qu.:251.0 SAT:0 13/09/2017:1 Median :301.0 SUN:0 16/06/2017:1 Mean :277.6 THU:0 27/10/2017:1 3rd Qu.:303.0 TUE:0 01/01/2018:0 Max. :315.0 WED:2 (Other) :0 .......would the above summary and str be useful? – AlGrasso Mar 02 '18 at 19:32
  • Hi, after further attempts I managed to spread the coordinates neatly in the x axis converting FTD_DATE col. using: zed$FTD_DATE <- as.Date(zed$FTD_DATE,format = "%d/%m/%Y"). The new problem is that the dates are now showed as integers in the x axis (0000,0010,0020, etc.)...When I use the typeof(FTD_DATE) function I get "double" whilst it used to be "integer". Basically FTD_DATE is still a read as a number and displayed wrongly in the x axis.. How can I finally make it look like what is supposed to be? A date? Anyone could help? Thanks... A – AlGrasso Mar 03 '18 at 14:39

0 Answers0