2016-09-12 10 views
0

私は、一貫した方法で名前を付けられた以前のforループで作った何百ものプロットを持っています。プロットは、タスクの結果がドメインにネストされています。forループとgrid.arrangeで不均等な数のプロットを結合する

grid.arrangeを使用して、同じプロット上に定義されたドメインのすべてのタスクをプロットしたいと思います。

domain_key <- data.frame(domain = c("soe", "soe", "soe", "elit", "elit"), 
        tasks = c("personal", "size", "shapeid", "onetoone", "puzzle")) 

dummy <- ggplot(data.frame()) + geom_point() + xlim(0, 10) + ylim(0, 100) 

plot.personalpct <- dummy + ggtitle("plot.personalpct") 
plot.sizepct <- dummy + ggtitle("plot.sizepct") 
plot.shapeidpct <- dummy + ggtitle("plot.shapeidpct") 
plot.onetoonepct <- dummy + ggtitle("plot.onetoonepct") 
plot.puzzlepct <- dummy + ggtitle("plot.puzzlepct") 

そして、ここでそれを行う方法の私の基本的な考え方です:私のデータおよびプロットの構造を説明

ダミーデータ

for(j in domain_key$domain){ 
    tasks <- unique(with(domain_key, tasks[domain == j])) #Get a list of the unique tasks for the domain 
    plots <- paste("plot.", tasks, "pct", sep ="") #Get the name of the plots as a character vector 
    grid.arrange(eval(parse(text = plots))) #evaluate the expression with grid arrange to display all the plots 
} 

私の問題は、ということだけで、最終的な引数です各ドメインの最初のプロットを表示します。これは、私の文字ベクトルは、コンマで区切られていないため、複数のオブジェクトとして解析されないためです。私は回避策の束を試したが、これを回避する方法を見つけることができません。または、私のアプローチが完全にオフかもしれません。

多くの援助に感謝します。

+0

多分ちょうど 'grid.arrange(grobs = MGET(LS(パターン= "プロット\\ .. *のpctを")))を行います'? – lukeA

+0

コメントありがとうございます。私は、すべての**私のgrobs( "プロット"で始まり、 "pct"で終わり、それらを1つのプロットに置く**)を取ると思う。私はそれぞれに関連するgrobsを含むいくつかのプロットを作る方法を探している私のキーで指定された**ドメイン**。 – jsizzle

答えて

1

多分これは@baptisteと@lukeAの助けを

library(ggplot2) 
library(gridExtra) 

dummy_plot <- function(id, fill) ggplot() + ggtitle(id) + 
    theme(panel.background = element_rect(fill = fill)) 

pl = list(`domain 1` = lapply(1:3, dummy_plot, fill = "#FBB4AE"), 
      `domain 2` = lapply(1:2, dummy_plot, fill = "#B3CDE3"), 
      `domain 3` = lapply(1:4, dummy_plot, fill = "#CCEBC5"), 
      `domain 4` = lapply(1:5, dummy_plot, fill = "#DECBE4")) 

dummy_group <- function(domain) arrangeGrob(grobs = pl[[domain]], top = domain) 
grid.arrange(grobs = lapply(names(pl), dummy_group)) 

enter image description here

0

感謝を助けます。どちらの提案も私の質問にはあまり答えませんでしたが、私を正しい道に導く助けとなりました。

私は実際にgrid.arrangeリストを供給する必要があることに気がついたので、妥当な、しかしひどく上品な解決策は見つけられませんでした。私はドメインの名前をそれぞれのタスクのプロット名に埋め込んだ。次に、grid.arrageとgrepを使ってプロットのリストをドメイン名で入力しました。これは合理的にうまくいく。

これは少しあまりにも多くの詳細を持っていますが、私の解決策を示しています

### Graph means of tasks by treatment group with error bars 

pltList <- list() #Create and empty list to store task plots 

for(outcome in unique(domain_key$task)){ 
    df <- data.frame(Intervention.Group = factor(unique(children$treatment)), 
       Percent.Correct = eval(parse(text = paste0("meansbytreatment$", outcome, "$estimates"))), 
       SE = eval(parse(text = paste0("meansbytreatment$", outcome, "$se")))) 
    df$upper <- df$Percent.Correct + 2*df$SE 
    df$lower <- df$Percent.Correct - 2*df$SE #Make a temp df with with the estimates and errors for each treatment group 

    domain <- unique(domain_key$domain[domain_key$task == outcome]) #extract the domain that the task belongs to 
    pltName <- paste("plot", outcome, domain, sep = ".") #Make a unique plot name with task and domain 

    pltList[[ pltName ]] <- ggplot(df, aes(Intervention.Group, Percent.Correct, fill = Intervention.Group)) + 
    geom_bar(stat = "identity") + 
    geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.4) + 
    ggtitle(paste(outcome, "by Intervention Group"))+ 
    theme(legend.position="none") #Save the plot to the plot list 
} 

### Graph domain subtasks together 

Domainplt <- lst() #Make an empty list to store plots of all the taks in a domain 

for(j in unique(domain_key$domain)){ 
    plots <- grep(j, names(pltList)) #get all of the plots from the domain 
    nplots <- length(plots) # get the n of plots for the domain 
    Domainplt[[j]] <- grid.arrange(grobs = pltList[plots], 
           ncol = nplots, 
           top = j) #Arrange all of the plots horizontally 
} 
関連する問題