R 中具有给定频率的条形图 [英] Bar chart in R with given frequencies
本文介绍了R 中具有给定频率的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 R 中为以下数据制作条形图:174 个蓝色 m&ms、224 个红色、230 个黄色、215 个橙色、195 个绿色和 216 个棕色 m&ms,全部放在一个袋子里.我被要求做的是:制作一个条形图,显示袋子中观察到的颜色的相对频率."但我不确定如何准确地做到这一点.谢谢
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
推荐答案
使用 barplot()
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))
或者,最好使用 ggplot2
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")
这篇关于R 中具有给定频率的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文