R中的Boxplot显示平均值

时间:2010-03-22 14:27:40

标签: r plot boxplot

是否有人知道在R中生成箱线图的方法,其中一行(或另一个符号)的值与平均值相对应?

谢谢!

5 个答案:

答案 0 :(得分:39)

abline(h=mean(x))

表示水平线(如果水平定位箱图,请使用v而不是垂直方向的h),或

points(mean(x))

一点。使用参数pch更改符号。您可能希望对它们进行着色以提高可见性。

请注意,在绘制箱线图后会调用它们。

如果使用公式界面,则必须构造均值向量。例如,从?boxplot获取第一个示例:

boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
means <- tapply(InsectSprays$count,InsectSprays$spray,mean)
points(means,col="red",pch=18)

如果您的数据包含缺失值,您可能希望将tapply函数的最后一个参数替换为function(x) mean(x,na.rm=T)

答案 1 :(得分:11)

使用ggplot2

p<-qplot(spray,count,data=InsectSprays,geom='boxplot')
p<-p+stat_summary(fun.y=mean,shape=1,col='red',geom='point')
print(p)

答案 2 :(得分:10)

从包PerformanceAnalytics检查chart.Boxplot。它允许您定义用于分布均值的符号。

alt text http://bm2.genes.nig.ac.jp/RGM2/R_current/library/PerformanceAnalytics/man/images/big_chart.Boxplot_001.png

答案 3 :(得分:0)

根据@James和@Jyotirmoy Bhattacharya的回答,我提出了这个解决方案:

zx <- replicate (5, rnorm(50))
zx_means <- (colMeans(zx, na.rm = TRUE))
boxplot(zx, horizontal = FALSE, outline = FALSE)
points(zx_means, pch = 22, col = "darkgrey", lwd = 7)

(有关详细信息,请参阅this帖子)

如果您想在水平框图中添加点数,请参阅this帖子。

答案 4 :(得分:0)

我也认为chart.Boxplot是最好的选择,它给你平均值的位置,但是如果你有一个带返回的矩阵,你需要的是一行代码来获得一个图中的所有箱图。

这是一个小型ETF投资组合示例。

library(zoo)
library(PerformanceAnalytics)
library(tseries)
library(xts)

VTI.prices = get.hist.quote(instrument = "VTI", start= "2007-03-01", end="2013-03-01",
                        quote = c("AdjClose"),provider = "yahoo",origin ="1970-01-01", 
                        compression = "m", retclass = c("zoo"))

VEU.prices = get.hist.quote(instrument = "VEU", start= "2007-03-01", end="2013-03-01",
                        quote = c("AdjClose"),provider = "yahoo",origin ="1970-01-01", 
                        compression = "m", retclass = c("zoo"))

VWO.prices = get.hist.quote(instrument = "VWO", start= "2007-03-01", end="2013-03-01",
                        quote = c("AdjClose"),provider = "yahoo",origin ="1970-01-01", 
                        compression = "m", retclass = c("zoo"))


VNQ.prices = get.hist.quote(instrument = "VNQ", start= "2007-03-01", end="2013-03-01",
                       quote = c("AdjClose"),provider = "yahoo",origin ="1970-01-01", 
                       compression = "m", retclass = c("zoo"))

TLT.prices = get.hist.quote(instrument = "TLT", start= "2007-03-01", end="2013-03-01",
                        quote = c("AdjClose"),provider = "yahoo",origin ="1970-01-01", 
                        compression = "m", retclass = c("zoo"))

TIP.prices = get.hist.quote(instrument = "TIP", start= "2007-03-01", end="2013-03-01",
                         quote = c("AdjClose"),provider = "yahoo",origin ="1970-01-01", 
                         compression = "m", retclass = c("zoo"))

index(VTI.prices) = as.yearmon(index(VTI.prices))
index(VEU.prices) = as.yearmon(index(VEU.prices))
index(VWO.prices) = as.yearmon(index(VWO.prices))

index(VNQ.prices) = as.yearmon(index(VNQ.prices))
index(TLT.prices) = as.yearmon(index(TLT.prices))
index(TIP.prices) = as.yearmon(index(TIP.prices))

Prices.z=merge(VTI.prices, VEU.prices, VWO.prices, VNQ.prices, 
           TLT.prices, TIP.prices)

colnames(Prices.z) = c("VTI", "VEU", "VWO" , "VNQ", "TLT", "TIP")

returnscc.z = diff(log(Prices.z))

start(returnscc.z)
end(returnscc.z)
colnames(returnscc.z) 
head(returnscc.z)

返回矩阵

ret.mat = coredata(returnscc.z)
class(ret.mat)
colnames(ret.mat)
head(ret.mat)

返回矩阵的框图

chart.Boxplot(returnscc.z, names=T, horizontal=TRUE, colorset="darkgreen", as.Tufte =F,
          mean.symbol = 20, median.symbol="|", main="Return Distributions Comparison",
          element.color = "darkgray", outlier.symbol = 20, 
          xlab="Continuously Compounded Returns", sort.ascending=F)

您可以尝试更改mean.symbol,并删除或更改median.symbol。 希望它有所帮助。 :)