如何从R导出几个图形

时间:2017-06-29 08:13:25

标签: r dataframe graph export lapply

我有数据框列表,我想每个数据框导出一个条形图...我正在尝试使用lapply但它不起作用...有谁知道怎么做?

my_data <- lapply(X = seq(from = 1, to = length(in_files_path), by = 1), FUN = function(x){
  data_tables <- read.table(file = in_files_path[[x]], header = TRUE)
})

lapply(X = seq(from = 1, to = length(in_files_path), by = 1), FUN = function(x){
  setwd(dir = ou_graph_path)
  png(filename = in_files_name[[x]], 
      units = "in", 
      width = 15, 
      height = 10, 
      res = 300)
  ggplot(data = my_data[[x]], aes(x = my_data[[x]]$A, y = my_data[[x]]$B)) +
    geom_bar()
  dev.off()
}) 

1 个答案:

答案 0 :(得分:1)

我建议使用以下方法

我建议使用以下方法

# Get list of files
# Start loop -
# read files
# make plot
# store plots in list
# - end loop
#
# Start loop -
# perform plot operation
# save plots
# - end loop

setwd(your_location_of_the_files)
list_files = list.files(pattern = ".csv")

for(i_file in list_files){
    dummy = fread(i_file,header = TRUE)
    png(filename = paste(your_location_for_the_plots,in_files_name[[x]],sep="/"), 
    units = "in", 
    width = 15, 
    height = 10, 
    res = 300)
    # You can just say A here, not dummy$A
    plot(ggplot(data = dummy, aes(x = A, y = B)) + geom_bar())
    dev.off()
}