在R中自动生成图形

时间:2015-10-26 22:39:58

标签: r csv

我根据特定格式调用了一系列csv文件:

4个prefices" matrix_del_cats _"," matrix_add_cats _"," matrix_del_groups _"," matrix_add_groups _",然后是重复计数0到9 ,其次是" _"和6个向量" [1,0,0,0,0,0]"到" [0,0,0,0,0,1]"

看起来像这个(非详尽的)列表:

matrix_add_cats_0_[1, 0, 0, 0, 0, 0].csv
matrix_add_cats_1_[1, 0, 0, 0, 0, 0].csv
matrix_add_cats_2_[1, 0, 0, 0, 0, 0].csv
...
matrix_add_cats_9_[1, 0, 0, 0, 0, 0].csv
matrix_add_cats_0_[0, 1, 0, 0, 0, 0].csv
matrix_add_cats_1_[0, 1, 0, 0, 0, 0].csv
matrix_add_cats_3_[0, 1, 0, 0, 0, 0].csv
...
matrix_add_cats_9_[0, 1, 0, 0, 0, 0].csv
...
matrix_add_cats_0_[0, 0, 1, 0, 0, 0].csv
...
matrix_add_cats_0_[0, 0, 0, 1, 0, 0].csv
...
matrix_add_cats_0_[0, 0, 0, 0, 1, 0].csv
...
matrix_add_cats_0_[0, 0, 0, 0, 0, 1].csv
...

每个csv文件如下所示:

  

名称,A,B,C,d,E,F,G,H,I,K,L,M,N,O,P,Q,R,S,T,U,V,W,X ,Y,Z,AA   A,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0, 0,0,0   B,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0, 0,0,0   C,0,0,0,0,0,0,0,7,5,7,0,0,0,5,0,0,7,5,0,5,7,5,0,0, 0,0,0   d,5,0,0,5,5,7,0,0,0,4,0,0,0,0,0,0,0,0,0,5,5,5,0,0, 0,0,0   E,0,0,0,5,0,0,0,5,0,5,7,0,0,0,0,0,0,0,0,0,5,0,0,0, 0,0,0   楼0,0,0,7,0,0,0,0,0,7,0,0,0,0,0,0,5,0,0,0,0,0,0,0, 0,0,0   G,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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5,0,0,0,0,0,5,5,0,0,5, 0,5,0   Z,0,0,0,0,0,0,0,0,5,5,0,0,5,0,0,0,0,0,0,5,7,0,0,7, 5,0,0   AA,0,0,0,0,0,0,0,5,5,0,5,0,5,7,7,0,0,5,0,5,5,0,0,0, 0,0,0

我需要自动:

  1. 阅读每个csv文件
  2. 生成具有特定标题的图形
  3. 并将其保存到保存名称的png文件而不是csv文件
  4. 标题应如下所示:

      

    """ + {"群组" OR"类别"。前者如果"组"   出现在csv文件中,后者如果" cats"出现在文件中   name} +"根据" + {A,B,C,D,E或F.使用" A" if [1,0,0,0,0,0]," B"如果[0,1,0,0,0,0等]}

    这是我个人形象的代码:

    library(ggplot2)
    install.packages("extrafont");library(extrafont)
    font_import(pattern = 'Akk') 
    library(reshape2)
    
    t1 <- read.csv("/matrix_add_cats_0_[1, 0, 0, 0, 0, 0].csv", check.names = FALSE, sep = ",")
    t2 <- read.csv("/matrix_del_cats_0_[1, 0, 0, 0, 0, 0].csv", check.names = FALSE, sep = ",")
    
    tableau <- cbind(t1[,1, drop=FALSE], t1[,-1] - t2[,-1])
    mylevels <- tableau$Name
    tableau.m <- melt(tableau)
    
    #reorder factors
    tableau.m$Name <- factor(tableau.m$Name,levels=mylevels)
    tableau.m$variable <- factor(tableau.m$variable, levels=mylevels)
    
    p <- ggplot(tableau.m, aes(variable,Name)) + 
          geom_tile(aes(fill = value), colour = "white") + 
          scale_fill_distiller(palette = "YlGnBu",limits=c(min(tableau.m$value), max(tableau.m$value))) +
          geom_text(aes(label=value), family="AkkuratLightPro-Regular", color = "black",lineheight=.5,size = 4)
    
    base_size <- 9
    p + theme_grey(base_size = base_size) + 
      labs(x = "", y = "") + scale_x_discrete(expand = c(0, 0)) + 
      scale_y_discrete(expand = c(0, 0)) + 
      theme(legend.position = "none", axis.ticks = element_blank(),
      axis.text.x = element_text(size = 12, angle = 270, hjust = 0, colour = "grey50", family="AkkuratPro-Regular")
      ,axis.text.y = element_text(size = 12, angle = 0, hjust = 1, colour = "grey50", family="AkkuratPro-Regular")) +
      ggtitle("***") + 
         theme(plot.title = element_text(size = 16, angle = 0, colour = "grey25", family="AkkuratPro-Regular"))
    
    ggsave(file="***.png")
    

    虽然这很复杂,但我很确定这可以在R中完成。有关如何进行的任何线索吗?

1 个答案:

答案 0 :(得分:2)

尝试这样的方法: 请查看var query = context .Table1 .SelectMany(table1 => context .Table2 .Where(table2 => table1.strStringContainsIntegers.Contains(table2.intInteger.ToString())) .Select( table2 => new {table1.SomeField, table2.SomeField})); ?lapply

获取文件

?mapply

将它们作为列表

读取
matrix_add_cats_files <- list.files("YOUR PATH", full.names = TRUE, pattern = "matrix_add_cats*")
matrix_del_cats_files <- list.files("YOUR PATH", full.names = TRUE, pattern = "matrix_del_cats*")

进行数据转换

dat_add_cats <- lapply(matrix_add_cats_files, read.csv, check.names = FALSE, sep = ",")
dat_del_cats <- lapply(matrix_del_cats_files, read.csv, check.names = FALSE, sep = ",")

所以你有一个包含所有画面数据的列表。

绘制

此示例将dat <- mapply(function(t1, t2){ tableau <- cbind(t1[,1, drop=FALSE], t1[,-1] - t2[,-1]) mylevels <- tableau$Name tableau.m <- melt(tableau) tableau.m$Name <- factor(tableau.m$Name,levels=mylevels) tableau.m$variable <- factor(tableau.m$variable, levels=mylevels) tableau.m }, dat_add_cats, dat_del_cats) 作为绘图标题和名称。 在其中放入matrix_add_cats_files的列表/向量,并附上您的名字。

length==length(dat)