2

I have a dataframe that only lists months from October to April. When I plot this data on a line graph, it includes the unused months as well. I would only like to show the months that are listed in the data so there is no unused space on the plot.

The code I am using for the plot

gplot(df,aes(GAME_DATE,DEF_RATING)) + 
   geom_line(aes(y=rollmean(df$DEF_RATING,9, na.pad = TRUE))) + 
   geom_line(aes(y=rollmean(df$OFF_RATING,9,na.pad = TRUE)),color='steelblue')

enter image description here

Sample data

    GAME_DATE OFF_RATING DEF_RATING
       <dttm>      <dbl>      <dbl>
 1 2017-04-12      106.1      113.1
 2 2017-04-10      107.1      100.8
 3 2017-04-08      104.4      105.1
 4 2017-04-06      116.1      105.9
 5 2017-04-04      116.9      116.0
jhaywoo8
  • 767
  • 5
  • 13
  • 23
  • Please could you provide some example data? Check out https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for help – Phil Nov 11 '17 at 18:30
  • It's unnecessary and undesirable to restate the data frame name inside `aes`. Use just the bare variable name: `geom_line(aes(y=rollmean(DEF_RATING,9, na.pad = TRUE)))`. – eipi10 Nov 11 '17 at 18:49
  • @Phil added example data – jhaywoo8 Nov 11 '17 at 19:31

2 Answers2

3

ggplot2 doesn't allow broken axes because such axes can be misleading. However, if you still want to proceed with this, you can simulate a broken axis with faceting. To do this, create a grouping variable to mark each "island" where data is present with a unique group code and then facet by those group codes.

Also, the data should be converted to long format before plotting, so that you can get two separate colored lines with a single call to geom_line. Mapping a column to color inside aes will also automatically generate a legend.

Here's an example with fake data:

library(tidyverse)

# Fake data
set.seed(2)
dat = data.frame(x=1950:2000, 
                 y1=c(cumsum(rnorm(30)), rep(NA,10), cumsum(rnorm(11))),
                 y2=c(cumsum(rnorm(30)), rep(NA,10), cumsum(rnorm(11))))

dat %>% 
  # Convert to long format
  gather(key, value, y1:y2) %>% 
  # Add the grouping variable
  group_by(key) %>% 
  mutate(group=c(0, cumsum(diff(as.integer(is.na(value)))!=0))) %>% 
  # Remove missing values
  filter(!is.na(value)) %>% 
  ggplot(aes(x, value, colour=key)) +
    geom_line() +
    scale_x_continuous(breaks=seq(1950,2000,10), expand=c(0,0.1)) +
    facet_grid(. ~ group, scales="free_x", space="free_x") +
    theme(strip.background=element_blank(),
          strip.text=element_blank())

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
0

You can try to delimiting x-axis with "scale_x_date()" for your present dates like this:

gplot(df,aes(GAME_DATE,DEF_RATING)) + 
geom_line(aes(y=rollmean(df$DEF_RATING,9, na.pad = TRUE))) + 
geom_line(aes(y=rollmean(df$OFF_RATING,9,na.pad = TRUE)),color='steelblue') +    
scale_x_date(date_labels="%b",breaks=seq(min(df$GAME_DATE),max(df$GAME_DATE), "1 month"))
Garmo
  • 131
  • 2