简体   繁体   English

在循环[R]中绘制多个分组的条形图

[英]Plotting Several Grouped Bar Plots in Loop [R]

my challenge is to plot several bar plots at once, a plot for each of variables of different subsets. 我的挑战是一次绘制多个条形图,每个不同子集的变量绘制一个图。 My goal is to compare regional differences for each variable. 我的目标是比较每个变量的区域差异。 I would like to print all the resulting plots to a html file via R Markdown. 我想通过R Markdown将所有结果图打印到html文件中。

My main difficulty in making automatic grouped bar charts is that you need to tabulate the groups using table(data$Var[i], data$Region) but I don't know how to do this automatically. 我制作自动分组条形图的主要困难是,您需要使用table(data$Var[i], data$Region)将组列表化table(data$Var[i], data$Region)但是我不知道如何自动执行此操作。 I would highly appreciate a hint on this. 我对此表示感谢。

Here is a an example of what one of my subset looks like: 这是我的一个子集的示例:

# To Create this example of data:
b <- rep(matrix(c(1,2,3,2,1,3,1,1,1,1)), times=10)  
data <- matrix(b, ncol=10)
colnames(data) <- paste("Var", 1:10, sep = "")
data <- as.data.frame(data)
reg_name <- c("North", "South")
Region <- rep(reg_name, 5)
data <- cbind(data,Region)

Using beside = TRUE , I was able to create one grouped bar plot (grouped by Region for Var1 from data): 使用beside = TRUE ,我可以创建一个分组的条形图(根据数据对Var1按Region分组):

tb <- table(data$Var1,data$Region) 
barplot(tb, main="Var1", xlab="Values", legend=rownames(tb), beside=TRUE,
        col=c("green", "darkblue", "red"))

I would like to loop this process to generate for example 10 plots for Var1 to Var10: 我想循环此过程以生成例如Var1到Var10的10个图:

for(i in 1:10){
     tb <- table(data[i], data$Region)
     barplot(tb, main = i, xlab = "Values", legend = rownames(tb), beside = TRUE, 
             col=c("green", "darkblue", "red"))
     }

R prefer the apply family of functions, therefore I tried to create a function to be applied: R更喜欢apply系列函数,因此我尝试创建一个要应用的函数:

fct <- function(i) {
      tb <- table(data[i], data$Region)
      barplot(tb, main=i, xlab="Values", legend = rownames(tb), beside = TRUE,
             col=c("green", "darkblue", "red"))
      }
sapply(data, fct)

I have tried other ways, but I was never successful. 我尝试了其他方法,但从未成功。 Maybe lattice or ggplot2 would offer easier way to do this. 也许latticeggplot2将提供更简单的方式来做到这一点。 I am just starting in R, I will gladly accept any tips and suggestions. 我只是从R开始,我会很乐意接受任何提示和建议。 Thank you! 谢谢!

(I run on Windows, with the most recent Rv3.1.2 "Pumpking Helmet") (我在Windows上运行,带有最新的Rv3.1.2“泵浦头盔”)

Given that you say "My goal is to compare regional differences for each variable", I'm not sure you've chosen the optimal plotting strategy. 假设您说“我的目标是比较每个变量的区域差异”,那么我不确定您是否选择了最佳绘图策略。 But yes, it is possible to do what you are asking. 但是可以,您可以按照自己的要求去做。

Here's the default plot you get with your code above, for reference: 这是上面的代码提供的默认图,以供参考:

在此处输入图片说明

If you want a list with 10 plots for each variable, you can do the following (with ggplot) 如果您想要一个包含每个变量10个图的列表,则可以执行以下操作(使用ggplot)

many_plots <-

  # for each column name in dat (except the last one)...
  lapply(names(dat)[-ncol(dat)], function(x) {

    this_dat <- dat[, c(x, 'Region')]
    names(this_dat)[1] <- 'Var'

    ggplot(this_dat, aes(x=Var, fill=factor(Var))) +
      geom_bar(binwidth=1) + facet_grid(~Region) +
      theme_classic()
  })

Sample output, for many_plots[[1]] : 示例输出,用于many_plots[[1]]

在此处输入图片说明

If you wanted all the plots in one image, you can do this (using reshape and data.table) 如果您希望所有绘图都在一幅图像中,则可以执行此操作(使用reshape和data.table)

library(data.table)
library(reshape2)
dat2 <- 
  data.table(melt(dat, id.var='Region'))[, .N, by=list(value, variable, Region)]

ggplot(dat2, aes(y=N, x=value, fill=factor(value))) +
  geom_bar(stat='identity') + facet_grid(variable~Region) +
  theme_classic()

在此处输入图片说明

...but that's not a great plot. ...但这不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM