Reputation: 797
I have to make a report of a survey where we have a question like this (all answers are checkboxes).
What is you favorite cake(s) (please choose more than one):
[] Tiramisù
[] Carrot Cake
[] Cupcake
Then the survey software exports into CSV like this:
"username","likes_tiramisu","likes_carrotcake","likes_cupcake"
"test01",1,1,1
"test02",0,1,1
"test03",0,1,0
"test04",0,0,1
I would like to make a barplot where every histogram represent the frequency of every cake. How can I combine a table like this:
"likes_tiramisu" "likes_carrotcake" "likes_cupcake"
1 3 3
Could I just solve it with sum of all elements in each column? Is it conceptually valid?
Upvotes: 0
Views: 345
Reputation: 9037
A ggplot2
approach would look like this:
library(ggplot2)
data.melt <- melt(data, id = "username")
qplot(variable, value, data = data.melt, geom = "bar", stat = "identity")
Upvotes: 2
Reputation: 60492
Suppose your data is in the file: survey.csv
Then to create a barplot use the following commands:
#Read in the data
d = read.table("survey.csv", sep=",", header=TRUE)
#Need to skip the username column, so d[,2:4]
#Use apply to calculate the totals in your table
barplot(apply(d[,2:4], 2, sum))
HTH
Upvotes: 3