title: ggplot2 data labels outside margins
body: <p>I'm making many figures in ggplot2 using a for loop, but my data labels are extending beyond the plot margin. I've tried using expand, but it only works for some figures. When I try to use <code>par(mar)</code> I get this error message: </p>
  
  <blockquote>
    <p>Error: Don't know how to add o to a plot.</p>
  </blockquote>
  
  <p>I also tried just using <code>ggsave</code> to save as a really wide file, but 1) that looks odd and 2) that won't work for making so many different figures.</p>
  
  <p>Does anyone know of any other workarounds? Ideally a way to have the inner plot margins automatically set per figure based on the length of the bars + data labels. Below is the code I'm using and an example figure (you can see the bar for 'x' is outside the margin). Thank you in advance!</p>
  
  <pre><code>for (i in each) {
    temp_plot = ggplot(data= subset(Data, Each == i)) + 
      geom_bar(stat = "identity", 
               aes(x = reorder(Letter, +Number), y = Number, fill = factor(Category))) + 
      xlab("Letters") + 
      ggtitle(paste0("Title"), subtitle = "Subtitle") + 
      coord_flip() + 
      theme_classic() + 
      theme(plot.title = element_text(hjust = 0.5, size=16), 
            plot.subtitle = element_text(hjust = 0.5)) +
      scale_fill_manual(values = c("#00358E", "#00AFD7"), 
                        name= "Category", 
                        labels=c("This","That")) +
      geom_text(family="Verdana", size=3, 
                aes(label=Number2, x=reorder(Letter, +Number), y=Number), 
                position=position_dodge(width=0.8), hjust=-0.001) + 
      scale_y_continuous(labels = comma, expand = c(0.01,0)) + 
      scale_x_discrete(labels = letters)
    ggsave(temp_plot, file=paste0("Example", i,".jpeg"))
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Ck9uR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ck9uR.jpg" alt="Example plot"></a></p>