2012-09-12 11 views
11

मैं जानना चाहता हूं कि geom_density() वास्तव में क्या कर रहा है, इसलिए मैं ग्राफ को औचित्य देता हूं और यदि फंक्शन के प्रत्येक वक्र के लिए उत्पन्न होने वाले फ़ंक्शन या पॉइंट निकालने का कोई तरीका है।आर - क्या एल्गोरिदम geom_density() उपयोग करता है और कैसे वक्र के अंक/समीकरण निकालने के लिए?

धन्यवाद

+0

मुझे लगता है कि stat_density पाया() आप पैरामीटर सेट करने के लिए अनुमति देता है। तो शायद वह पहले भाग का जवाब देता है। अभी भी जानना चाहते हैं कि समीकरण या अंक निकाले जा सकते हैं या नहीं। – unixsnob

उत्तर

18

get("compute_group", ggplot2::StatDensity) टाइप (या, पूर्व, get("calculate", ggplot2:::StatDensity)) आप घनत्व की गणना करने के लिए इस्तेमाल एल्गोरिथ्म मिल जाएगा। (रूट पर, यह kernel="gaussian" डिफ़ॉल्ट के साथ density() के लिए एक कॉल है।)

साजिश में इस्तेमाल अंक अदृश्य print.ggplot() द्वारा दिया जाता है, ताकि आप उन्हें इस तरह उपयोग कर सकते हैं:

library(ggplot2) 
m <- ggplot(movies, aes(x = rating)) 
m <- m + geom_density() 
p <- print(m) 
head(p$data[[1]], 3) 
#   y  x density scaled count PANEL group ymin  ymax 
# 1 0.0073761 1.0000 0.0073761 0.025917 433.63  1  1 0 0.0073761 
# 2 0.0076527 1.0176 0.0076527 0.026888 449.88  1  1 0 0.0076527 
# 3 0.0078726 1.0352 0.0078726 0.027661 462.81  1  1 0 0.0078726 


## Just to show that those are the points you are after, 
## extract and use them to create a lattice xyplot 
library(gridExtra) 
library(lattice) 
mm <- xyplot(y ~x, data=p$data[[1]], type="l") 

enter image description here

3

जैसा कि अन्य उत्तरों में सुझाया गया है, आप print.ggplot() का उपयोग कर ggplot बिंदुओं तक पहुंच सकते हैं। हालांकि, print() -ing कोड भी ggplot ऑब्जेक्ट प्रिंट करता है, जिसे वांछित नहीं किया जा सकता है।

आप साजिश मुद्रण के बिना ggplot वस्तु डेटा, निकालने, ggplot_build() का उपयोग कर प्राप्त कर सकते हैं:

library(ggplot2) 
library(ggplot2movies) 

m <- ggplot(movies, aes(x = rating)) 
m <- m + geom_density() 
p <- ggplot_build(m) # <---- INSTEAD OF `p <- print(m)` 
head(p$data[[1]], 3) 
#    y  x  density  scaled count  n PANEL group ymin 
# 1 0.007376115 1.000000 0.007376115 0.02591684 433.6271 58788  1 -1 0 
# 2 0.007652653 1.017613 0.007652653 0.02688849 449.8842 58788  1 -1 0 
# 3 0.007872571 1.035225 0.007872571 0.02766120 462.8127 58788  1 -1 0 


# Just to show that those are the points you are after, extract and use them 
# to create a lattice xyplot 
library(lattice) 
m2 <- xyplot(y ~x, data=p$data[[1]], type="l") 

library(gridExtra) 
grid.arrange(m, m2, nrow=1) 

enter image description here

+1

धन्यवाद, मेगाट्रॉन, यह वही है जो मैं ढूंढ रहा था! – Luis

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