2012-11-25 88 views
5

我運行一些模擬,我在想繪製的結果在一個美麗的ggplot列表對象,但似乎ggplot不能對付列表對象。有誰知道如何將結果粘貼到ggplot圖表中?密謀使用ggplot

N <- 8619170   
    nn <- c(1000, 1200, 3000) 
    p <- .27  
    nsim <- 100 

    phat <- list() 
    for (i in 1:length(nn)) { 
    n <- nn[i] 
    x <- rhyper(nsim, N * p, N * (1 - p), n) 
    phat[[i]] <- x/n 
    } 

醜陋溶液:

names(phat) <- paste("n=", nn) 
    stripchart(phat, method="stack") 
    abline(v=p, lty=2, col="red") 
+0

一旦你有你在GGPLOT2喜歡的格式列表,請參閱一些例子在哪裏?geom_dotplot –

+0

,我認爲這個問題是一個真正的問題,即如何繪製使用ggplot列表給出的數據。 – highBandWidth

回答

7

GGPLOT2需要一個data.frame作爲源數據。所以,你需要:

  1. 與reshape2(或plyr或許多其他工具)
  2. 陰謀使用qplot轉換數據或ggplot

    ## transform data 
    require(reshape2) 
    h <- do.call(cbind, phat) 
    h.melt <- melt(h) 
    
    ## rename variables so they look nicer on plots 
    names(h.melt) <- c("test","N","value")  
    
    ## stripchart (not shown) 
    qplot(data = h.melt, x = value,y = N,color=N)+geom_point() 
    
    ## histogram (not shown)  
    ggplot(h.melt,aes(x=value,fill=N))+geom_histogram()+facet_grid(N~.) 
    
    ## dotplot with rug (not shown) 
    ggplot(h.melt,aes(x=value,fill=N))+geom_dotplot()+facet_grid(N~.)+geom_rug() 
    
    ##density plot with rug (shown below) 
    ggplot(h.melt,aes(x=value,fill=N))+geom_density()+facet_grid(N~.)+geom_rug() 
    

    enter image description here

+2

只是這個答案即興過一點,因此他們希望在劇情更好,並嘗試直方圖,密度圖,我們點陣圖可能命名列,也許加上地毯。喜歡的東西: '名稱(h.melt)< - C( 「測試」, 「N」, 「值」) ggplot(h.melt,AES(X =值,填補= N))+ geom_histogram()+ facet_grid(N〜)+ geom_rug() ggplot(h.melt,AES(X =值,填補= N))+ geom_dotplot()+ facet_grid(N〜)+ geom_rug() ggplot(h.melt, AES(X =值,填補= N))+ geom_density()+ facet_grid(N〜)+ geom_rug()' – MattBagg

1

我能做FOL最好降脂您的線索是:

qplot(data = h.melt, x = value,y = Var2)+ geom_point(shape=1, size=5) 

但仍不能反映概率;點應該堆積爲一種直方圖來反映概率。

一種不同的方法是使用密度函數,但它可以亂七八糟的東西,如果我有許多樣品類別繪製出來。

ggplot(h.melt, aes(x=value, fill=Var2)) + geom_density(alpha=.5, position="identity")