首页 文章

一个或多个循环 - 多个ggplot图r

提问于
浏览
0

最后几天我确实问了一些关于循环的问题,这对我帮助很大,非常感谢你们!我想,这次我遇到了更复杂的问题 . 我跳过了标签代码中的标签和一些细节,但它完美无缺 .

一般来说,我想用循环产生大量的情节 . 我学会了如何处理像this这样的简单情节,但是对于ggplot和aes,我发现它更复杂 .

Here are the data as dropbox上的.csv .

一些准备:

data <- select(baza,strefa,v1,v2,v3,v4,v5,v6,v7,v8) # with dplyr package
strefa <- data$strefa
data$strefa <- NULL
data <- data.frame(apply(data, 2, function(x) {x <- recode(x, "9=NA"); x})) #with car package

一些总结为aes:a - - data $ v1 df < - data.frame(cbind(a,strefa))

srednie <- aggregate(a ~ strefa, data = df, FUN = mean)
GD.mean <- round(mean(a, na.rm = TRUE), digits = 2)
srednie <- rbind(srednie, c(13,GD.mean))

和ggplot代码:

jpeg(paste0("plot_",names(data)[i],".jpg"),
         width = 166, height = 120, units = "mm", pointsize = 12,
         res = 300, quality = 90)
     ggplot(srednie, aes(x=factor(strefa), y=a, label=round(srednie$a, digits = 2))) +
          geom_bar(position=position_dodge(), stat="identity", fill="#fff68f", colour="darkgrey", width = 0.5) +
          theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank()) +
          geom_text(size = 4, hjust = 1.2) +
          coord_flip(ylim = c(1,6))              
     dev.off()

如何将其绑定到for循环?它应该是一个循环还是更多?

for (i in 1:length(names(data))) {

    df <- data.frame(cbind(i,strefa))
    srednie <- aggregate(i ~ strefa, data = df, FUN = mean)
    GD.mean <- round(mean(i, na.rm = TRUE), digits = 2)
    srednie <- rbind(srednie, c(13,GD.mean))

jpeg(paste0("plot_",names(data)[i],".jpg"),
                 width = 166, height = 120, units = "mm", pointsize = 12,
                 res = 300, quality = 90)
      ggplot(srednie, aes(x=factor(strefa), y=i, label=round(srednie$i, digits = 2))) +
            geom_bar(position=position_dodge(), stat="identity", fill="#fff68f", colour="darkgrey", width = 0.5) +
            theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank()) +
            geom_text(size = 4, hjust = 1.2) +
            coord_flip(ylim = c(1,6))

 dev.off()
    }

1 回答

  • 1

    由于您的代码似乎按预期工作,因此您确实没有问题 . 这只是对其他尝试方法的建议 .

    如果您打算使用 dplyr ,那就一直走 . 在R中有许多不同的循环方式 . 我个人倾向于避开 for 循环并使用apply( applylapply', sapply')函数或 dplyr .

    我没有一个投递箱账户而且我不会创建一个账户,因此我无法使用您的数据 . 但是,这是一个你应该能够适应的例子 .

    Packages:

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    

    Data Generation: 随机数据,我只想制作一些简单的条形图 .

    Data <- data.frame(Groups=LETTERS[1:5],
                       Var1 = rchisq(5,1),
                       Var2 = rchisq(5,1),
                       Var3 = rchisq(5,1))
    

    Plotting 'Loop':

    首先,为情节创建一个函数:

    plt.fnc <- function(frm){
      PLT <- ggplot(frm,aes(x=Groups,y=Value)) +
        geom_bar(stat='identity') +
        xlab(frm$Variable[1])
    
      ggsave(PLT,file=paste0('PLOT_',frm$Variable[1],'.jpeg'),width=10,height=7.5)
    
      return(PLT)
    }
    

    这是绘图的主力 . 注意使用 ggsave 而不是 jpeg / dev.off . 功能是相同的,但ggsave往往使代码更清洁 . 此功能既可以生成绘图并保存它 . 当你的代码开始变得混乱时,将循环的内容放入函数可能是一个很好的做法,因为它可以让你保持更清洁的流程 .

    接下来,我使用dplyr而不是使用循环 .

    Plots <- Data %>%
      gather(Variable,Value,Var1:Var3) %>% # this is equivilent to melt.
      group_by(Variable) %>%               # itterates over values of variable
      do(Plot = plt.fnc(.))                # Calls our plotting function.
    

    此方法的主要问题是您必须弄清楚如何将绘图中所需的所有信息合并到一个数据帧中 . 如果不可能,那么通常应用函数或 for 循环会更好 .

相关问题