Reputation: 2579
I am trying to make a few barplots in R. Here is what I have so far:
For this particular chart it is not important to me how many responses there are per browser, I simply want to know which percentage was a yes and which percentage was a no for each browser.
How can I transform the table that I am using to use 'percentage of column' instead of number of results? Is there a way to transform each column by the sum of that column? (I am very new to R) Thanks!
Edit: Sorry for lack of information. I have not installed any extra packages, just r-base on Ubuntu (installed this morning). Here is the output of dput:
structure(c(11L, 32L, 3L, 4L, 1L, 0L, 1L, 1L), .Dim = c(2L, 4L
), .Dimnames = structure(list(c("No", "Yes"), c("Chrome", "Firefox",
"Internet Explorer", "Safari")), .Names = c("", "")), class = "table")
Edit2: I found this function which seems to do what I want but am not really sure why it works.
prop.table(mymat, margin=2)*100
Upvotes: 1
Views: 998
Reputation: 42639
Assumes two levels
barplot(x / rep(apply(x, 2, sum),each=2))
> apply(x,2,sum)
Chrome Firefox Internet Explorer Safari
43 7 1 2
This is the margins over the columns. It gives the column sums. Now if we want to divide X by these values, it has to be in the right order. The data is in column-major order, so when used as a vector for division, apply(x,2,sum)
will recycle over the wrong elements -- transposed, if you will.
Setting each=2
in the rep
command applies one element of the result of the apply
call to each column (by duplicating the element). Thus the assumption that the number of levels is 2.
For a more general solution, you could use each=nrows(x)
.
Upvotes: 2
Reputation: 56905
You could just calculate the percentage and plot it?
Percentage = Yes/total votes, data['Yes', ]
selects the 'Yes' column and colSums
calculates totals per column, so:
barplot( data['Yes', ]/colSums(data) )
Is that what you meant?
Upvotes: 2