0

I am trying to display a limited number of observations in the text of the x axis, but maintaining the entire population of discrete observations in the plot:

zed <- structure(list(FTD = c(93L, 50L, 88L), FTD_DATE = structure(c(12L, 15L, 2L), .Label = c("01/03/2018", "03/06/2017", "05/05/2017", "08/01/2018", "08/12/2017", "09/07/2017", "11/10/2017", "13/02/2018", "14/09/2017", "15/11/2017", "16/05/2017", "17/11/2017", "18/09/2017", "18/12/2017", "21/07/2017", "21/10/2017", "23/06/2017", "26/05/2017"), class = "factor")), .Names = c("FTD", "FTD_DATE"), row.names = c(NA, 3L), class = "data.frame")

library(ggplot2)
ggplot(data=zed, aes(x=FTD_DATE,y=FTD)) +
  theme(axis.text.x = element_text
        (angle = 90, vjust = 0.5, hjust = 1.0)) +
  geom_point(aes(x=FTD_DATE,y=FTD)) +
  scale_x_discrete (breaks=c("05/05/2017","03/06/2017","09/07/2017",
                             "11/10/2017","08/12/2017","08/01/2018",
                             "01/03/2018"))

enter image description here

The above gives me a concentration of text in the left of the x axis (7 dates as per breaks argument in the scale_x_discrete, out of 19 dates in total) but leaving a wide blank space in the remaining portion of the axis... Is there a way to spread evenly the text across the entire x axis, choosing the number of observation I want to display in the text, maintaining the entire number of observation in the plot and ordering the dates in the x axis text in ascending order? Bearing in mind that scale_x_date returns error "Error: Invalid input: date_trans works with objects of class Date only".. Thanks

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
AlGrasso
  • 1
  • 3
  • 1
    Could you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Your question would have a higher chance of being answered. – missuse Mar 04 '18 at 10:25
  • Apologies if my question was too generic, Hope this can help? dput(head(zed,3))*** structure(list(FTD = c(93L, 50L, 88L), FTD_DATE = structure(c(12L, 15L, 2L), .Label = c("01/03/2018", "03/06/2017", "05/05/2017", "08/01/2018", "08/12/2017", "09/07/2017", "11/10/2017", "13/02/2018", "14/09/2017", "15/11/2017", "16/05/2017", "17/11/2017", "18/09/2017", "18/12/2017", "21/07/2017", "21/10/2017", "23/06/2017", "26/05/2017"), class = "factor")), .Names = c("FTD", "FTD_DATE"), row.names = c(NA, 3L), class = "data.frame")***zed is my dataset: head(zed,1).... FTD FTD_DATE 1 93 17/11/2017 – AlGrasso Mar 04 '18 at 12:45
  • You need make sure the minimal example captures the problem. I just updated the question with your dataset, but I don't think it is capturing the error you are explaining? Please use the way I have structured the code as a template if you need to update the question – Michael Harper Mar 04 '18 at 13:08
  • Hi Mikey thanks for your input, the graph above actually reproduces the issue: as you can see there are 3 dots in the scatterplot and one vertical axis text only (03/06/2017). The entire dataset (zed) is made of 19 dots (19 observations) and I wish to keep them all in the scatterplot but showing only 6 or 7 x axis texts in the x axis. My goal is to spread the text in the x axis evenly from left to right and to order the FTD_DATE dates in ascending order (which is to say the first and the last texts must be placed at the extreme left and right of the x axis, showing min and max values).. – AlGrasso Mar 04 '18 at 14:44

1 Answers1

0

Your dates are currently text in dd/mm/yyyy form which cannot be ordered correctly as they stand. You can convert the FTD_DATE column to a true date format, then use the scale_x_date method of ggplot2 to set whatever period is appropriate for the range of dates you are displaying.

Test Data

I've expanded your test data from three points to 18, keeping the labels you had in your struct but with some dummy FTD values:

zed <- data.frame(FTD = rep(c(93L, 50L, 88L, 66L, 75L, 94L), 3), 
                  FTD_DATE = c("01/03/2018", "03/06/2017", "05/05/2017", "08/01/2018", "08/12/2017", "09/07/2017", "11/10/2017", "13/02/2018", "14/09/2017", "15/11/2017", "16/05/2017", "17/11/2017", "18/09/2017", "18/12/2017", "21/07/2017", "21/10/2017", "23/06/2017", "26/05/2017"))

Conversion to date

zed$FTD_DATE = as.Date(zed$FTD_DATE, "%d/%m/%Y")

ggplot - breaks set to two weeks

library(ggplot2)
ggplot(data=zed, aes(x=FTD_DATE,y=FTD)) +
  theme(axis.text.x = element_text
        (angle = 90, vjust = 0.5, hjust = 1.0)) +
  geom_point(aes(x=FTD_DATE,y=FTD)) +
  scale_x_date(date_breaks = "2 weeks")

enter image description here

Breaks set to month

ggplot(data=zed, aes(x=FTD_DATE,y=FTD)) +
  theme(axis.text.x = element_text
        (angle = 90, vjust = 0.5, hjust = 1.0)) +
  geom_point(aes(x=FTD_DATE,y=FTD)) +
  scale_x_date(date_breaks = "month")

enter image description here

As you see, the points are plotted in ordered form left to right as you want, and the choice of date_breaks controls the number of labels shown on each plot.

Stewart Ross
  • 1,034
  • 1
  • 8
  • 10