2012-03-31 18 views
6

साजिश बनाते समय मैं अधिकतम हिस्टोग्राम मूल्य की गणना कैसे कर सकता हूं?अधिकतम हिस्टोग्राम मान की गणना

मैं एक एनोटेशन के साथ एक साजिश पर एक लाइन रखना चाहता हूं, और मैं चाहता हूं कि पाठ y-axis अधिकतम मान के आनुपातिक स्थिति हो। उदाहरण के लिए:

library(ggplot2) 
df <- data.frame(x = runif(1000)) 


p <- ggplot(data=df, aes(x)) + geom_histogram() 
p + geom_vline(aes(xintercept=0.5),color='red') + geom_text(aes(0.55, 10, label='line'), angle = 90, color='red') 

निम्नलिखित का उत्पादन:

enter image description here

मैं के रूप में मुझे लगता है कि इस स्थिति का सबसे अच्छा तरीका है geom_text() जो अधिकतम हिस्टोग्राम मूल्य की 1/3 है एक तर्क पारित करने के लिए चाहते हैं पाठ लगातार, लेकिन मुझे नहीं पता कि इस count मान की गणना कैसे करें।

उत्तर

3

stat_bin डिफ़ॉल्ट रूप से binwidth = रेंज/30 का उपयोग करता है। मुझे यकीन है कि इसकी गणना वास्तव में कैसे नहीं कर रहा हूँ, लेकिन यह एक काफी उचित सन्निकटन होना चाहिए:

max(table(cut(df$x,seq(min(df$x),max(df$x),dist(range(df$x))/30)))) 
+0

1/3 से गुणा करना न भूलें :) –

1

सामान्य रूप से, एक साधारण 1-आयामी अधिकतम खोज खोज निम्नानुसार लागू की जाती है (मेरे मामले में, एएनएसआई-सी में);

#include <stdio.h> 
#include <errno.h> 
int printMaxHistValue(int* yValues, int* xValues, int numPoints) { 
    int i, currentY=0, currentX=0, maxX=0, maxY=0, maxIndex=0; 

    if(numPoints <= 0) { 
    printf("Invalid number of points in histogram! Need at least 1 point! Exiting"); 
    return EINVAL; 
    } 


    // Find the values 
    for(i=0; i<numPoints; i++) { 
    currentX = xValues[i]; 
    currentY = yValues[i]; 
    if(currentY > maxY) { 
     maxY = currentY; 
     maxX = currentX; 
     maxIndex = i; 
    } 
    } 

    // Finished with search 
    printf("Found the maximum histogram value of y=%d at bin/x-value of %d (which corresponds to i=%d)",maxY,maxX,maxIndex); 

    // Done 
    return EOK; 
} 

आशा इस उदाहरण में मदद करता है :)

1

आप hist समारोह है, जो मायने रखता है की गणना करता है इस्तेमाल कर सकते हैं। बस सुनिश्चित करें कि आप इसे geom_histogram के रूप में एक ही बिन ब्रेक पास करते हैं। Geom_histogram को एक binwidth प्रदान नहीं करने के मामले में यह सीमा/30 के लिए डिफ़ॉल्ट है। कैसे geom_histogram डिब्बे उत्पन्न करता है पर देख रहे हैं से मुझे लगता है कि यह काम करना चाहिए:

require(plyr) 
min.brea <- round_any(min(df$x), diff(range(df$x))/30, floor) 
max.brea <- round_any(max(df$x), diff(range(df$x))/30, ceiling) 
breaks <- seq(min.brea, max.brea, diff(range(df$x/30))) 
histdata <- hist(df$x, breaks=breaks, plot=FALSE, right=FALSE) 
max.value <- max(histdata$counts) 

round_any समारोह plyr से है।

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