2012-11-25 43 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")