title: stacked barplot converting a variable into a presence absence based percentage for unrelated variables in ggplot2 R
body: <p>Following is a sample data frame</p>
  
  <pre><code>df &lt;- data.frame(SampleID = c(1, 2, 3, 4, 5, 6, 7, 8),
                   Var1 = c(0.1 , 0.5,    0.7,    0,  0,  0,  0.5,    0.2), 
                   Var1PA = c("Present", "Present", "Present", "Absent", "Absent", "Absent",  "Present", "Present"), 
                   Var2 = c(0, 0, 0, 0, 0.1, 0.5, 0.7, 0.2), 
                   Var2PA = c("Absent", "Absent", "Absent", "Absent", "Present", "Present", "Present", "Present"))
  </code></pre>
  
  <p>My question started off as seemingly simple, but I could not find a way to edit the dataframe suitably to plot a barplot.</p>
  
  <p><strong>For Var1, I want to plot a stacked barplot of the percent of times var1 was present in the sample (i.e var1 value > 0) or absent (Similarly for var2 and so on).</strong> </p>
  
  <p>I could determine this percentage by:</p>
  
  <pre><code>(1 - sum(df$Var1 == 0) / length(df$Var1)) * 100
  </code></pre>
  
  <p>But how do I convert this into a percentage while plotting?
  I looked at many melt options, but there is no unifying criteria for these variables that would make a common X axis</p>
  
  <p>Finally, how does one answer the question above if I want to plot 5 variables from a dataframe of 1000 such column variables?</p>
  
  <p>Edit: Thanks for the answers so far! I have a slight edit to the question
  I just added one more variable to my data frame</p>
  
  <pre><code>df &lt;- data.frame(SampleID = c(1, 2, 3, 4, 5, 6, 7, 8),
               Var1 = c(0.1 , 0.5,    0.7,    0,  0,  0,  0.5,    0.2), 
               Var1PA = c("Present", "Present", "Present", "Absent", "Absent", "Absent",  "Present", "Present"), 
               Var2 = c(0, 0, 0, 0, 0.1, 0.5, 0.7, 0.2), 
               Var2PA = c("Absent", "Absent", "Absent", "Absent", "Present", "Present", "Present", "Present"),
               Disease = c("Case", "Control", "Case", "Control", "Case", "Control", "Case", "Control"))
  </code></pre>
  
  <p>I am trying to figure out how to plot the barplot for cases and controls with presence absence stacked within them for Var1PA, Var2PA and so on. If I have the right data frame input, the ggplot2 code would be :
      vars &lt;- c('Var1PA', 'Var2PA', 'Var2PA')
      ##based on the first comment by @rawr
      tt &lt;- data.frame(prop.table(as.table(sapply(df[, vars], table)), 2) * 100)
      ggplot(tt, aes(Disease, Freq)) +<br>
      geom_bar(aes(fill = Var1), position = "stack", stat="identity") +     facet_grid(~vars)</p>
  
  <p>How do I get percentages for cases (present and absent) and controls (present and absent) for each of the vars? Thanks!</p>