Home > Back-end >  R plot: Quarterly date not in sequential order
R plot: Quarterly date not in sequential order

Time:08-30

sorry for the lack of reproducibility example that I am about to display but I am sure this issue has happened quite a lot when building plots. I have two time series that I want to display (one as a barplot and the other as a line). I also want to use two different Y-axes scales. To do so I use the following code:

Color <- "#FF6347"
priceColor <- rgb(0.2, 0.6, 0.9, 1)
coeff <- 0.035

ggplot(data, aes(x=time, group =1))  
  geom_bar( aes(y=YseriesA), stat="identity", size=.1, fill=Color, color="black", alpha=.4)   
  geom_line( aes(y=YseriesB / coeff), size=1.2, color=priceColor)  
  scale_y_continuous(
    # Features of the first axis
    name = "B",
    # Add a second axis and specify its features
    sec.axis = sec_axis(~.*coeff, name="A")
  )   
  theme_ipsum()  
  theme(
    axis.title.y = element_text(color = Color, size=13),
    axis.title.y.right = element_text(color = priceColor, size=13)
  )  
  theme(axis.text.x = element_text(angle = 90))  
  ggtitle("Title")

Most importantly, the variable time I have produced with the following code:

data$time <- seq(from = zoo::as.yearqtr("2000-01-01"), to = zoo::as.yearqtr("2019-01-01"), by = 1/4) %>%
  format("Q%q-%Y")

enter image description here

My question, the issue is the following. Why on earth is time not ordered from earlier to later in the X-axes but displays this weird order: Q1-2000, Q1-2001, Q1-2002..... Any help much much appreciated and once again my most sincere apologies for the lack of reproducibility.

CodePudding user response:

It is fully linked to the order of the variable. Here the order is alphabetic, this is why 'Q1-2018' is prior to 'Q2-2000'. You could either rename your modalities in this format: 2018-Q1 or use the factor function with level argument to specify the right order you want.

The following code is an example, you can use all modalities from your variable time.

### Simulation of data and checking order
data <- data.frame(time=c("Q1-2018", "Q2-2010", "Q3-2000"))
factor(data$time)
# -> Levels: Q1-2018 Q2-2010 Q3-2000

### Reordering levels and checking order
data$time <- factor(data$time, levels=c("Q3-2000", "Q2-2010", "Q1-2018"))
factor(data$time)
# -> Levels: Q3-2000 Q2-2010 Q1-2018

Another solution is to use the as.yearqtr function on your dates.

yourDates <- c("2018-02-02", "2010-05-05", "2000-10-10")
data <- as.yearqtr(yourDates, format = "%Y-%m-%d")
factor(data)
# -> Levels: 2000 Q4 2010 Q2 2018 Q1

CodePudding user response:

You could use zoo's scale_x_yearqtr (now that you're using that class!):

library(ggplot2)
library(zoo)

df |>
  ggplot(aes(x = date, y = y))   
  geom_line()  
  theme(axis.text.x = element_text(angle = 90))  
  zoo::scale_x_yearqtr(n = 40,
                         format = "Q%q-%Y")

Output

enter image description here

Data (please provide some another time - it doesn't need to be perfect):

library(tibble)
library(zoo)

set.seed(1)
df <- tibble(y = rnorm(77),
             date = seq(from = zoo::as.yearqtr("2000-01-01"),
                        to   = zoo::as.yearqtr("2019-01-01"),
                        by   = 1/4)
             )
  • Related