Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please try to post your data in an easy to copy-and-paste format, like I've done below:</p> <pre><code>mydata &lt;- structure(list(col1 = c(84L, 94L, 188L, 245L, 114L, 71L, 118L, 162L, 123L, 115L, 125L, 119L, 106L, 146L), col2 = c(88.242, 107.571, 240.288, 371.005, 131.599, 100.751, 138.543, 203.435, 152.032, 126.945, 134.178, 138.926, 129.19, 162.319), col3 = c(9.833, 10.917, 16.917, 22.333, 9.167, 8.167, 11.167, 14.667, 12.167, 11.667, 10, 9.5, 9.833, 9.833), col4 = c(4.194, 3.708, 6.333, 10.389, 4.25, 3, 4.278, 6.444, 4.639, 5.056, 4.639, 4.222, 3.833, 4.118), cr = structure(c(1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor")), .Names = c("col1", "col2", "col3", "col4", "cr"), class = "data.frame", row.names = c(NA, -14L)) </code></pre> <p>Now. to address your question. You need to first <code>aggregate</code> the data, then convert it to a <code>matrix</code>, then calculate each value in the matrix as a proportion to the total in that column (using <code>prop.table</code>):</p> <pre><code>mydataAgg &lt;- aggregate(cbind(col1, col2, col3, col4) ~ cr, mydata, sum) mydata2 &lt;- as.matrix(mydata1[-1]) rownames(mydata2) &lt;- mydataAgg[[1]] mydata2 # col1 col2 col3 col4 # A 1235 1527.057 110.250 46.673 # B 575 697.967 55.918 22.430 prop.table(mydata2, 2) # col1 col2 col3 col4 # A 0.6823204 0.6863103 0.6634851 0.6754121 # B 0.3176796 0.3136897 0.3365149 0.3245879 </code></pre> <p>Plotting is then easy:</p> <pre><code>barplot(prop.table(mydata2, 2)) </code></pre> <p>Or, with colors:</p> <pre><code>barplot(prop.table(mydata2, 2), col = c("slateblue", "palevioletred")) </code></pre> <p><img src="https://i.stack.imgur.com/goMMM.png" alt="enter image description here"></p> <p>Hmmm. Not the most interesting plot, but I guess definitely a clear pattern in proportions....</p> <hr> <h2><code>lattice</code></h2> <p>@Arun showed the <code>ggplot2</code> solution in the name of completeness, but if that's the case, then we should at least add <code>barchart</code> from "lattice". ;)</p> <p>For this, we need to transpose the output of <code>prop.table(mydata2, 2)</code> that we calculated earlier:</p> <pre><code>barchart(t(prop.table(mydata2, 2)), stack = TRUE, horizontal = FALSE) </code></pre> <p>Here's the result:</p> <p><img src="https://i.stack.imgur.com/KPsZn.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload