Reputation: 541
I need to make a bar graph in R for the following data: 174 blue m&ms, 224 red, 230 yellow, 215 orange, 195 green, and 216 brown m&ms all in one bag. What What I'm asked to do is: "Make a bar chart of the observed relative frequency of colors in the bag." But I'm not sure how to exactly to do this. Thanks
Upvotes: 0
Views: 3612
Reputation: 347
Use barplot()
data <- c(rep("blue",174),rep("red",224),rep("yellow",230),rep("orange",215),rep("green",195),rep("brown",216))
t <- table(data)
barplot(t/sum(t), col=names(t))
or, better use ggplot2
library(ggplot2)
data <- c(rep("blue",174),rep("red",224),rep("yellow",230),rep("orange",215),rep("green",195),rep("brown",216))
df <- data.frame(mnm=data)
ggplot(df, aes(x=mnm)) + geom_histogram(aes(y=(..count..)/sum(..count..),fill=mnm)) + scale_fill_manual(name="M&M", values=sort(as.character(unique(df$mnm)))) + ylab("Relative Frequency")
Upvotes: 0