2010-03-22 12 views
22

दिखा रहा है क्या किसी को किसी पंक्ति (या किसी अन्य प्रतीक) के साथ आर में बॉक्सप्लॉट उत्पन्न करने के तरीके के बारे में पता है?आर में बॉक्सप्लॉट औसत

धन्यवाद!

उत्तर

35
abline(h=mean(x)) 
एक क्षैतिज रेखा के लिए

(ऊर्ध्वाधर के लिए उपयोग वी के बजाय ज अगर आप अपने boxplot क्षैतिज रूप से समन्वित), या

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) 

हैं, तो आप function(x) mean(x,na.rm=T)

+0

क्या आप बता सकते हैं कि यह बॉक्सप्लॉट {ग्राफिक्स} उदाहरण के उदाहरण के उदाहरण में कैसे किया जा सकता है? बॉक्सप्लॉट (गिनती ~ स्प्रे, डेटा = कीटस्प्रे, कॉल = "लाइटग्रे") – Brani

+0

मैंने इसे अब एक उदाहरण के रूप में उत्तर में जोड़ा है – James

8

साथ tapply समारोह के अंतिम तर्क को बदलने के लिए ggplot2 के साथ चाहते हो सकता है:

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

@ जेम्स और @ ज्योतिर्मॉय भट्टाचार्य के उत्तरों के आधार पर मैं इस समाधान के साथ आया:

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 पोस्ट देखें।

0

मुझे यह भी लगता है कि चार्ट। बॉक्सप्लॉट सबसे अच्छा विकल्प है, यह आपको माध्य की स्थिति देता है, लेकिन यदि आपके पास रिटर्न के साथ मैट्रिक्स है तो आपको एक ग्राफ़ में सभी बॉक्सप्लॉट प्राप्त करने के लिए कोड की एक पंक्ति है।

यहां एक छोटा ईटीएफ पोर्टफोलियो उदाहरण है।

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 बदल जाते हैं। उम्मीद है कि यह मदद की। :)

संबंधित मुद्दे