Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Setting limits to scale_x_yearqtr in ggplot for yearqtr (zoo)

I'm working with the data set resembling the extract below:

head(nomis.lng.agg)
  quarter decile           avg.val
1 2004 Q4                1 5.680000
2 2005 Q1                1 5.745763
3 2005 Q2                1 5.503341
4 2005 Q3                1 5.668224
5 2005 Q4                1 5.244604
6 2006 Q1                1 5.347222

The variable quarter is of class yearqtr as generated by zoo. The remaining two columns are numerical. I'm currently generating a plot that uses the following ggplot syntax:

ggplot(data = subset(x = df,
                     subset = df$decile== 1 |
                         df$decile== 10),
       aes(x = quarter, y = avg.val, group = decile)) +
    geom_line(aes(linetype=as.factor(decile)),
              size = 1) +
    scale_x_yearqtr(format = "%YQ%q", n  = 5) +
    xlab("Quarter") +
    ylab("Average val") +
    ggtitle("Plot") +
    scale_linetype_discrete(name="Legend") +
    theme(panel.background = element_blank(),
          axis.line = element_line(colour = "black"),
          axis.text = element_text(size = 12, colour = "black"),
          axis.title = element_text(size = 14, colour = "black"),
          panel.grid.minor = element_blank(),
          panel.grid.major.y = element_line(colour = "gray"),
          panel.grid.major.x = element_blank(),
          axis.text = element_text(size = 12, colour = "black"),
          legend.text = element_text(size = 12),
          legend.title = element_text(size = 12),
          legend.key.width=unit(1.5,"cm"),
          legend.position = "bottom",
          legend.key = element_rect(fill = "white"),
          legend.background = element_rect(colour = "black"),
          plot.title = element_text(face="bold"),
          plot.background = element_rect(colour = "black"))

The plot is almost perfect with exception of the x-axis. The current x-axis looks like that: broken axis

My focus is on the code scale_x_yearqtr(format = "%YQ%q", n = 5). As my data starts from 2004 Q4 I'm not interested in plotting 2004 Q1, but I want to set the limits with:

scale_x_yearqtr(format = "%YQ%q", 
                    limits=c(min(quarter), max=max(quarter)))

However, this doesn't yield the desired results, despite:

min(df$quarter)
[1] "2004 Q4"
like image 823
Konrad Avatar asked Feb 23 '15 16:02

Konrad


1 Answers

I think you just didn't specify the limits properly. In addition, to achieve greater control over the appearance, use the breaks argument (rather than n).

# some data
df <- data.frame(x = as.yearqtr(2004 + seq(3, 8)/4), y = sample(1:6)) 

# setting limits only
ggplot(data = df, aes(x, y, group = 1)) +
  geom_line() +
  scale_x_yearqtr(limits = c(min(df$x), max(df$x)),
                  format = "%YQ%q")

enter image description here

# setting breaks
ggplot(data = df, aes(x, y, group = 1)) +
  geom_line() +
  scale_x_yearqtr(breaks = seq(from = min(df$x), to = max(df$x), by = 0.25),
                  format = "%YQ%q")

enter image description here

like image 105
Henrik Avatar answered Oct 21 '22 01:10

Henrik