title: Conditional grouped barplot R
body: <p>I am trying to make a <code>barplot</code> in R for two categorical variables, <code>Dep_meds_at_time_of_rx_2</code> and <code>phq9_cat</code>. <code>phq9_cat</code> has two levels, <code>0</code> and <code>1</code>, where <code>0</code> corresponds to <code>PHQ-L</code> and <code>1</code> corresponds to <code>PHQ-H</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code># get counts of vars
  counts &lt;- table(data2$Dep_meds_at_time_of_rx_2, data2$phq9_cat)
  
  # get percentages of vars
  pcnts &lt;- scale(counts, FALSE, colSums(counts))*100
  
  # plot barplot
  bp &lt;- barplot(pcnts, beside=TRUE, col=c("azure3", "azure4"), ylab="Frequency (%)", border=NA)
  legend("topright", legend=c("PHQ-L", "PHQ-H"), bty="n", fill=c("azure3", "azure4"), border=NA)
  text(bp, 1, round(pcnts, 2), cex=1, pos=3, col=c("black"))
  </code></pre>
  
  <p>And the resulting plot:</p>
  
  <p><a href="https://i.stack.imgur.com/RyfHJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RyfHJ.jpg" alt="enter image description here"></a></p>
  
  <p>Which is great! But I need to only plot the <code>data2$Dep_meds_at_time_of_rx_2==1</code> category. So I would like a barplot with only the <code>3.03</code> bar and the <code>19.44</code> bar. </p>
  
  <p>I've exhausted any clever tricks that I know of already such as making the <code>data2$Dep_meds_at_time_of_rx_2==0</code> bars white and using <code>space = c(-1, 0)</code> to make the <code>data2$Dep_meds_at_time_of_rx_2==1</code> bars next to one another but then the bars are super wide, like so:</p>
  
  <p><a href="https://i.stack.imgur.com/FsIVo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FsIVo.jpg" alt="enter image description here"></a></p>
  
  <p>I just need the <code>data2$Dep_meds_at_time_of_rx_2==1</code> columns, but at a normal width. </p>
  
  <p>Any ideas?</p>
  
  <p>Here is my data:</p>
  
  <pre><code>&gt; dput(data2)
  structure(list(Dep_meds_at_time_of_rx_2 = c(0L, 0L, 0L, 0L, 0L, 
  0L, 0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