title: Dynamic limits and breaks in scale_y_continuous
body: <p>How do I have dynamic limits and breaks in my ggplot based on my input subset. My code works like this,</p>
  
  <pre><code>Listed %&gt;%
    filter(Listed$Country == 'USA' &amp; Listed$period &gt; max(Listed$period) - months(60)) %&gt;%
    mutate(Months_year = format(as.Date(period), "%b")) %&gt;%
    mutate(fill = ifelse(Months_year %in% past3months,"A","B")) %&gt;%
    ggplot(aes(x = variable, y = value,fill = fill)) + guides(fill=FALSE) +
    geom_bar(stat = "identity") +
    theme_classic() +
    labs(x = "",y="") +
    ggtitle("Newly Listed") + 
    theme(plot.title = element_text(hjust = 0.5,face="bold"))+
    scale_x_date(labels = date_format("%b-%Y"), date_breaks  ="2 month",     
    expand = c(0.005,0)) + 
    scale_y_continuous(limits=c(0,max(Listed$value)), 
                       breaks  = seq(0,max(Listed$value), by = 2000),
    expand = c(0,0))+
    theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5))
  </code></pre>
  
  <p>But here, the <code>max(Listed$value)</code> does not consider the filter I have applied <code>filter(Listed$Country == 'USA' &amp; Listed$period &gt; max(Listed$period) - months(60))</code> - which is only USA and past 5 years. Do I need to apply the same filters again within the max function ? Or any other way to make use of the existing pipe data ?</p>
  
  <p>Let me know if any other further information is required. I don't know why people down vote here instead of asking for clarification.</p>
  
  <p><strong>EDIT</strong>. -- I tried to use just the column name, </p>
  
  <pre><code>limits=c(0,max(value)), 
                       breaks  = seq(0,max(value), by = 2000)
  </code></pre>
  
  <p>But getting this error - <code>Error in seq.default(0, max(value), by = 2000) : object 'value' not found</code></p>
  
  <p>Sample data..</p>
  
  <blockquote>
    <p>head(Listed)</p>
  </blockquote>
  
  <pre><code>  Country   period    value
      USA    2007-01-01   704
      UK     2007-01-01  3621
      AU     2007-01-01   776
      USA    2007-02-01  1015
      AU     2007-02-01    71
     China   2007-03-01   485
     .
     .
     .
  </code></pre>