如何在R中绘制平均图 [英] How to plot average graph in R
问题描述
我有不同的宿舍
Quarter GrossMargin
2009 Q1 17.60%
2009 Q1 17.80%
2010 Q2 18.50%
2011 Q1 21.60%
像这样我有大数据集.我想在 R 中绘制它.我使用了 qplot(df$Quarter, df$GrossMargin, binwidth=.5)
See like this i have big data set. I want to plot this in R. I used qplot(df$Quarter, df$GrossMargin, binwidth=.5)
它出现在每次出现的数据点上.就像在 x 轴 2009 Q1 中一样,Y 轴上相应地有两个数据点,一个在 17.6,另一个在 17.8.但我想要每个季度的平均值,就像 x 轴 2009 Q1 应该对应于 Y 轴的单个值(即 17.6 和 17.8 的平均值).如果我尝试做 mean(GrossMargin),它会给出整列的平均值,这是没有用的.我所有的季度(2009-Q1、Q2、Q3、2010-Q1、Q2、Q3)都在一列中,而相应的 GrossMargin 值在另一列中.如何在 R 中做到这一点
It came dots at the data points of each occurance. Like in x-axis 2009 Q1 there are correspondingly two data points in Y-axis one at 17.6 and other at 17.8. But i want an average value for each quarter, like in x-axis 2009 Q1 should correspond to the single value in Y-axis (that is average of 17.6 and 17.8). If i try to do mean(GrossMargin), it gives mean of whole column, which is of no use. All my quarters(2009-Q1,Q2,Q3, 2010-Q1,Q2,Q3) are in one column and corresponding values of GrossMargin in other single column. How to do this in R
Quarter GrossMargin
2009 Q1 17.60%
2009 Q1 17.80%
2009 Q1 22.40%
2009 Q1 17.70%
2009 Q1 17.50%
2009 Q1 17.50%
2009 Q1 18.40%
2009 Q1 18.70%
2009 Q1 21.10%
2009 Q1 13.60%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 18.50%
2009 Q1 17.70%
2009 Q1 18.20%
2009 Q1 30.20%
2009 Q1 17.60%
2009 Q1 19.00%
2009 Q1 14.60%
2009 Q1 17.50%
2009 Q1 18.40%
2009 Q1 21.60%
2009 Q1 21.10%
2009 Q1 18.40%
2009 Q1 17.30%
2009 Q1 14.60%
2009 Q1 17.60%
2009 Q1 17.50%
2009 Q2 17.50%
2009 Q1 14.60%
2009 Q1 17.60%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 17.30%
2009 Q1 17.60%
2009 Q1 18.50%
2009 Q1 18.20%
2009 Q1 17.50%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 30.20%
2009 Q1 17.60%
2009 Q1 18.40%
2009 Q1 30.20%
2009 Q1 18.50%
2009 Q1 39.30%
2009 Q1 17.50%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 17.30%
2009 Q1 19.40%
2009 Q1 20.90%
2009 Q1 17.70%
2009 Q1 18.70%
2009 Q1 17.50%
2009 Q1 17.70%
2009 Q1 17.50%
2009 Q1 17.50%
2011 Q1 18.20%
2011 Q1 17.60%
2011 Q1 17.70%
2011 Q1 17.70%
2011 Q1 18.50%
2011 Q1 19.00%
2011 Q1 21.60%
2011 Q1 17.50%
2011 Q1 14.60%
2011 Q1 30.20%
2011 Q1 18.40%
2009 Q1 21.10%
2010 Q2 17.50%
2010 Q2 17.70%
2010 Q2 17.70%
2010 Q2 17.50%
2010 Q2 17.50%
2010 Q2 18.20%
2010 Q2 18.50%
2010 Q2 17.70%
2010 Q2 17.60%
2010 Q2 17.30%
2010 Q2 14.60%
2010 Q2 17.70%
2010 Q2 18.40%
2010 Q2 17.60%
2009 Q1 17.70%
2009 Q1 19.40%
2009 Q1 17.50%
2009 Q1 17.50%
2009 Q1 20.90%
2009 Q1 19.00%
2009 Q1 17.60%
2009 Q1 17.70%
2009 Q1 19.00%
2009 Q1 17.70%
2009 Q1 17.50%
2009 Q1 17.50%
2009 Q1 17.50%
其他 5 列是 Order.id 客户 ID、日期等
the are other 5 columns which are Order.id customer id, date etc
推荐答案
首先,将 %
字符串转换为数值:
First, transform the %
strings to numeric values:
df[2] <- as.numeric(gsub("%", "", as.character(df[ , 2])))
计算每个Quarter
的平均GrossMargin
:
dat <- aggregate(GrossMargin ~ Quarter, df, mean)
剧情:
plot(as.factor(dat$Quarter), dat$GrossMargin)
这篇关于如何在R中绘制平均图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!