2011-06-20 12 views
9

मैं एक्स वाई विमान में लक्ष्य के स्थान के एक समारोह के रूप में प्रतिक्रिया समय का मतलब (या अन्य कार्य) प्लॉट करना चाहता हूं। परीक्षण डाटा के रूप में:क्या जीजीपीएलओटी डेटा के 2 डी सारांश बना सकता है?

library(ggplot2) 
xs <- runif(100,-1,1) 
ys <- runif(100,-1,1) 
rts <- rnorm(100) 
testDF <- data.frame("x"=xs,"y"=ys,"rt"=rts) 

मैं जानता हूँ कि मैं यह कर सकता:

p <- ggplot(data = testDF,aes(x=x,y=y))+geom_bin2d(bins=10) 

मैं ऐसा करने में सक्षम होने के लिए क्या चाहते हैं, एक ही बात है, लेकिन प्रत्येक बिन में डेटा के एक समारोह साजिश गणना के बजाए। क्या मैं यह कर सकता हूं?

या मैं आर (जैसे drt <- tapply(testDF$rt,list(cut(testDF$x,10),cut(testDF$y,10)),mean)) में पहली सशर्त साधन उत्पन्न करने के लिए और फिर उस साजिश की आवश्यकता है?

धन्यवाद।

उत्तर

1

यह मेरी अपेक्षा से कठिन हो गया।

आप कर सकते हैं यह कर, एक weights सौंदर्य प्रदान करके में लगभग चाल ggplot, लेकिन वह सिर्फ तुम बिन, नहीं मतलब में वजन की राशि देता है (और आप नकारात्मक बिन मूल्यों को बनाए रखने के drop=FALSE निर्दिष्ट करना)। आप बिन के भीतर या तो गणना या घनत्व भी प्राप्त कर सकते हैं, लेकिन उनमें से कोई भी वास्तव में समस्या हल नहीं करता है।

यहाँ मैं के साथ समाप्त हो गया है:

## breaks vector (slightly coarser than the 10x10 spec above; 
## even 64 bins is a lot for binning only 100 points) 
bvec <- seq(-1,1,by=0.25) 

## helper function 
tmpf <- function(x,y,z,FUN=mean,breaks) { 
    midfun <- function(x) (head(x,-1)+tail(x,-1))/2 
    mids <- list(x=midfun(breaks$x),y=midfun(breaks$y)) 
    tt <- tapply(z,list(cut(x,breaks$x),cut(y,breaks$y)),FUN) 
    mt <- melt(tt) 
    ## factor order gets scrambled (argh), reset it 
    mt$X1 <- factor(mt$X1,levels=rownames(tt)) 
    mt$X2 <- factor(mt$X2,levels=colnames(tt)) 
    transform(X, 
      x=mids$x[mt$X1], 
      y=mids$y[mt$X2]) 
} 

ggplot(data=with(testDF,tmpf(x,y,rt,breaks=list(x=bvec,y=bvec))), 
     aes(x=x,y=y,fill=value))+ 
    geom_tile()+ 
    scale_x_continuous(expand=c(0,0))+ ## expand to fill plot region 
    scale_y_continuous(expand=c(0,0)) 

यह बराबर बिन चौड़ाई, आदि, बढ़ाया जा सकता है मान लिया गया है ... यह वास्तव में है कि बहुत बुरा है (जहाँ तक मैं बता सकता हूँ) stat_bin2d नहीं करता ' उपयोगकर्ता द्वारा निर्दिष्ट फ़ंक्शन को स्वीकार नहीं करते हैं।

+1

मैं "वस्तु 'एक्स' नहीं मिला" मिलता है, और जब मैं (एक्स के लिए एक्स 'बदलने में बदल) ', मुझे लगता है "eval में त्रुटि (expr, envir, enclos): ऑब्जेक्ट' mids 'नहीं मिला"। –

11

अद्यतन ggplot2 0.9.0 के रिलीज के साथ, इस कार्यक्षमता का ज्यादा stat_summary2d और stat_summary_bin के नए संयोजन से आच्छादित है।

StatAggr2d <- proto(Stat, { 
    objname <- "aggr2d" 
    default_aes <- function(.) aes(fill = ..value..) 
    required_aes <- c("x", "y", "z") 
    default_geom <- function(.) GeomRect 

    calculate <- function(., data, scales, binwidth = NULL, bins = 30, breaks = NULL, origin = NULL, drop = TRUE, fun = mean, ...) { 

    range <- list(
     x = scales$x$output_set(), 
     y = scales$y$output_set() 
    ) 

    # Determine binwidth, if omitted 
    if (is.null(binwidth)) { 
     binwidth <- c(NA, NA) 
     if (is.integer(data$x)) { 
     binwidth[1] <- 1 
     } else { 
     binwidth[1] <- diff(range$x)/bins 
     } 
     if (is.integer(data$y)) { 
     binwidth[2] <- 1 
     } else { 
     binwidth[2] <- diff(range$y)/bins 
     }  
    } 
    stopifnot(is.numeric(binwidth)) 
    stopifnot(length(binwidth) == 2) 

    # Determine breaks, if omitted 
    if (is.null(breaks)) { 
     if (is.null(origin)) { 
     breaks <- list(
      fullseq(range$x, binwidth[1]), 
      fullseq(range$y, binwidth[2]) 
     ) 
     } else { 
     breaks <- list(
      seq(origin[1], max(range$x) + binwidth[1], binwidth[1]), 
      seq(origin[2], max(range$y) + binwidth[2], binwidth[2]) 
     ) 
     } 
    } 
    stopifnot(is.list(breaks)) 
    stopifnot(length(breaks) == 2) 
    stopifnot(all(sapply(breaks, is.numeric))) 
    names(breaks) <- c("x", "y") 

    xbin <- cut(data$x, sort(breaks$x), include.lowest=TRUE) 
    ybin <- cut(data$y, sort(breaks$y), include.lowest=TRUE) 

    if (is.null(data$weight)) data$weight <- 1 
    ans <- ddply(data.frame(data, xbin, ybin), .(xbin, ybin), function(d) data.frame(value = fun(d$z))) 

    within(ans,{ 
     xint <- as.numeric(xbin) 
     xmin <- breaks$x[xint] 
     xmax <- breaks$x[xint + 1] 

     yint <- as.numeric(ybin) 
     ymin <- breaks$y[yint] 
     ymax <- breaks$y[yint + 1] 
    }) 
    } 
}) 

stat_aggr2d <- StatAggr2d$build_accessor() 

और उपयोग:

ggplot(data = testDF,aes(x=x,y=y, z=rts))+stat_aggr2d(bins=3) 
ggplot(data = testDF,aes(x=x,y=y, z=rts))+ 
    stat_aggr2d(bins=3, fun = function(x) sum(x^2)) 

enter image description here

https://gist.github.com/1341218

यहाँ इतनी के रूप में मनमाने ढंग से समारोह को स्वीकार करने के stat_bin2d के एक मामूली संशोधन है:

यहाँ इस उत्तर के लिए एक सार है

साथ ही, यहां एक स्लेज है stat_binhex की टी संशोधन:

StatAggrhex <- proto(Stat, { 
    objname <- "aggrhex" 

    default_aes <- function(.) aes(fill = ..value..) 
    required_aes <- c("x", "y", "z") 
    default_geom <- function(.) GeomHex 

    calculate <- function(., data, scales, binwidth = NULL, bins = 30, na.rm = FALSE, fun = mean, ...) { 
    try_require("hexbin") 
    data <- remove_missing(data, na.rm, c("x", "y"), name="stat_hexbin") 

    if (is.null(binwidth)) { 
     binwidth <- c( 
     diff(scales$x$input_set())/bins, 
     diff(scales$y$input_set())/bins 
    ) 
    } 

    try_require("hexbin") 

    x <- data$x 
    y <- data$y 

    # Convert binwidths into bounds + nbins 
    xbnds <- c(
     round_any(min(x), binwidth[1], floor) - 1e-6, 
     round_any(max(x), binwidth[1], ceiling) + 1e-6 
    ) 
    xbins <- diff(xbnds)/binwidth[1] 

    ybnds <- c(
     round_any(min(y), binwidth[1], floor) - 1e-6, 
     round_any(max(y), binwidth[2], ceiling) + 1e-6 
    ) 
    ybins <- diff(ybnds)/binwidth[2] 

    # Call hexbin 
    hb <- hexbin(
     x, xbnds = xbnds, xbins = xbins, 
     y, ybnds = ybnds, shape = ybins/xbins, 
     IDs = TRUE 
    ) 
    value <- tapply(data$z, [email protected], fun) 

    # Convert to data frame 
    data.frame(hcell2xy(hb), value) 
    } 


}) 

stat_aggrhex <- StatAggrhex$build_accessor() 

और उपयोग:

ggplot(data = testDF,aes(x=x,y=y, z=rts))+stat_aggrhex(bins=3) 
ggplot(data = testDF,aes(x=x,y=y, z=rts))+ 
    stat_aggrhex(bins=3, fun = function(x) sum(x^2)) 

enter image description here

+1

+1 इसे पोस्ट करने के लिए धन्यवाद। मैं इस पर ध्यान से अध्ययन करूंगा क्योंकि मैंने यह संशोधन करने की कोशिश की लेकिन असफल रहा। – Andrie

+0

+1 यह बहुत अच्छा लग रहा है! शायद यह 'स्पष्टता के लिए उपयोग के उदाहरण में बदल रहा है' समारोह (एक्स) '' कार्य करने के लिए (z) के लायक होगा। – Gregor

+0

@ कोहस्के: बस एक नोट।आपके फॉर्मूला और उदाहरण को आपके विशेषज्ञता के स्तर के बिना उन लोगों के लिए समायोजित नहीं किया जाता है। –

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