title: Setting axes limits based on variable values in ggplot
body: <p>I have a dataframe with programs and a country for each person in the program. I need to make a bar plot for each program with counts of participants from each country. I am including sample data and the code below. </p>
  
  <p>In reality I have over 20 programs with hundreds of entries for each country and would like to automate this process to iterate over all of the programs by only changing the name of the program in the code. In the bar plot, I need to have data labels on the outer ends of the horizontal bars, however this prints them beyond the limits of the plot window. To solve this, I would like to adjust the scale of the axis, however instead of adjusting it manually for each program, I would like to set the limit to 10% above the maximum value of the 'Count' variable.</p>
  
  <p>My question is whether it is possible to use variable values from a dataframe to set the limits instead of hardcoding them for each plot (and preferably doing this without saving a separate table for country counts). If this is not something that can be done, does anyone know of efficient alternative methods to automate axes scale adjustments and/or adjusting the plot window to fit the labels?</p>
  
  <p>I could not find an answer to this elsewhere, but if a similar question has already been answered, I would be grateful if someone can point me towards the solution. </p>
  
  <pre><code>sample_data &lt;- data.frame(
                            "person_number" = 1:15, 
                            "country" = c("United States", "Canada", "India", "United States", "United 
                                           States", "United States", "India", "China", "China", "United 
                                           States", "China", "India", "Canada", "United States", "China"), 
                            "program" = c("Program1", "Program2", "Program3", "Program2", "Program3", 
                                          "Program3", "Program3", "Program2", "Program3", "Program1", 
                                          "Program1", "Program3", "Program1", "Program2", "Program1")
                            )
  </code></pre>
  
  <pre><code>sample_data %&gt;% 
      filter(program == "Program1") %&gt;% 
      group_by(country) %&gt;% 
      summarise(Count = n_distinct(person_number)) %&gt;% 
      ggplot(aes(x = reorder(country, Count), y = Count)) +
        geom_bar(stat = "identity", fill = "salmon3") +
        coord_flip() +
        labs(title = "Country for Program1 Applicants") +
        geom_text(aes(label=Count), size=3, hjust=-0.2) +
        ylim(0, max(Count)+0.1*max(Count))                     # this line does not work
                                                               # error message: object 'Count' not found
  </code></pre>