R-将图分配给可变名称列表 [英] R - Assigning Plot to Variably Named List

查看:100
本文介绍了R-将图分配给可变名称列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将绘图分配给通过变量( snm )命名的列表.我的代码段是我为使其工作而尝试进行的所有变体.我还缺少其他什么选择?谢谢.

I am trying to assign a plot to a list that is named via variable (snm). My code snippet is all the variations that I tried to do to make it work. What other option am I missing? Thanks.

我的目标是遍历我的图形分配,使用IF语句更改 snm 和我将在图形中使用的其他一些变量.

My goal is to loop over my graph assignments, using an IF statement to change the snm and a few other variables that I will use in the graphs.

for (x in seq(0,1)) {
  if (x==0) {
    snm="grad"
  } 
  else if (x==1) {
    snm="start"
  }

    assign(snm,list(),envir=.GlobalEnv) #works
    assign(snm[[1]],ggplot(data=TDSF, aes(x=Graduation))+geom_histogram()+labs(title="A"),envir=.GlobalEnv) #works
    assign(snm[[2]],ggplot(data=TDSF, aes(x=Graduation,weights=Donation))+geom_bar()+labs(title="B"),envir=.GlobalEnv) #fails "subscript out of bonds"
    assign(snm[[3]],ggplot(data=TDSF, aes(x=State,weights=Donation))+geom_bar()+labs(title="B")+scale_y_sqrt(),envir=.GlobalEnv) #fails "subscript out of bonds"
    grid.arrange(grad[[1]],grad[[2]],grad[[3]])
}

基于@MrFlick和@hrbrmstr的部分解决方案,但是1)我必须在循环中使用do.call或得到相同的图形,以及2)看到mapply我觉得我应该能够使用它,但是不能使它正常工作.

Partial solution based on @MrFlick and @hrbrmstr, but 1) I have to use do.call in the loop or I get the same graphs and 2) seeing mapply I feel that I should be able to use it, but cannot get it to work.

library(ggplot2)
library(gridExtra)

set.seed(1492)
TDSF <- data.frame(Graduation=sample(1950:2010, 30),
                   Donation=sample(300:10000, 30),
                   Start.Year=sample(1950:2010,30),
                   State=sample(state.abb,30,replace=TRUE))

plots <- list()
for (x in seq(0,1)) {
  if (x==0) {
    nm=quote(Graduation)
    snm="grad"
  } 
  else if (x==1) {
    nm=quote(Start.Year)
    snm="start"
  }
  plots[[snm]]<-list()
  plots[[snm]][[1]] <- ggplot(data=TDSF, aes(x=eval(nm)))+geom_histogram()+labs(title=paste("Number of People per",snm,"Year"))
  plots[[snm]][[2]] <- ggplot(data=TDSF, aes(x=eval(nm),weights=Donation))+geom_bar()+labs(title=paste("Donations by",snm,"Year"))
  plots[[snm]][[3]] <- ggplot(data=TDSF, aes(x=State,weights=Donation))+geom_bar()+labs(title="Donations by State")+scale_y_sqrt()
}

do.call(grid.arrange,plots[["grad"]])
do.call(grid.arrange,plots[["start"]])

尝试尝试:

plot<-mapply(function(snm,nm) list(
  {ggplot(data=TDSF, aes(x=nm))+geom_histogram()+labs(title=paste("Number of People per",snm,"Year"))},
  {ggplot(data=TDSF, aes(x=nm,weights=Donation))+geom_bar()+labs(title=paste("Donations by",snm,"Year"))},
  {ggplot(data=TDSF, aes(x=State,weights=Donation))+geom_bar()+labs(title="Donations by State")+scale_y_sqrt()}
), c("grad","start"),c("Graduation","Start.Year"),SIMPLIFY = FALSE)

do.call(grid.arrange,plot[["grad"]])
do.call(grid.arrange,plot[["start"]])

推荐答案

尽管有很长的代码段,但您的问题确实不清楚.您是否正在尝试做这样的事情?

Despite a lengthy code snippet, your question really isn't clear. Are you trying to do something like this?

library(ggplot2)
library(gridExtra)

set.seed(1492)
TDSF <- data.frame(Graduation=sample(1950:2010, 30),
                   Donation=sample(300:10000, 30))

snm <- mapply(function(x, title) {
  ggplot(TDSF, aes_(x=as.name(x))) + 
    geom_histogram() + 
    labs(title=title) 
}, c("Graduation", "Donation"), c("A", "B"), SIMPLIFY=FALSE)

do.call(grid.arrange, snm)

这篇关于R-将图分配给可变名称列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆