2015-11-27 74 views
2
情節轉換

我們如何轉換從enter image description here箱線圖出錯R中

# Boxplot of MPG by Car Cylinders 
boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", 
    xlab="Number of Cylinders", ylab="Miles Per Gallon") 

產生的誤差曲線箱線圖(爲第25和第75位數),用最小的努力? enter image description here

+0

http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_%28ggplot2%29/ – Keniajin

回答

2

@Keniajin的鏈接爲您提供了正確的軌道,但您需要分位數函數。這是一個ggplot解決方案:

require(ggplot2) 

首先,我們創建函數來計算quantiles25 EN 75

Q25 <- function(x) {quantile(x, .25)} 
Q75 <- function(x) {quantile(x, .75)} 

然後我們做的情節,用stat_summary而這些功能。請注意,如果您願意,您可以用stderr的中位數,最小值和最大值替換該函數。

ggplot(data=mtcars, aes(x=cyl,y=mpg)) + 
    stat_summary(fun.y=mean,fun.ymin=Q25, fun.ymax=Q75) + 
    ggtitle("Car Milage Data") + 
    xlab("Number of Cylinders") + 
    ylab("Miles Per Gallon")