【问题标题】:I would like to read data from an output file and create stacked bar graphs?我想从输出文件中读取数据并创建堆叠条形图?
【发布时间】:2014-07-08 13:23:43
【问题描述】:

这是我使用的代码,因为我是 R 的新手,我使用了一些我在网上找到的东西来创建图表,但是我对其中的大部分内容进行了硬编码,现在我只想让它从文件中读取信息并创建这些图表。我现在的代码是这样的:

        ylabels <-  c( "sampleXYZ",
        "sampleXY",
        "sampleG",
        "sampleF",
        "sampleE",
        "sampleD",
        "sampleC",
        "sampleB",
        "sampleA"
        )

       tablex = read.table(file="testGraphData.txt", header=FALSE, sep="\t")
       dataframe <- as.data.frame.matrix(tablex)

       test <- matrix(c(dataframe[1,2],dataframe[2,2],dataframe[3,2],dataframe[4,2],dataframe[5,2],dataframe[6,2],dataframe[7,2],dataframe[8,2],dataframe[9,2],5,10,20,25,30,35,40,45,50), 
              nrow =2 , 
           ncol=9, 
           byrow=TRUE,
           dimnames = list(c("Calculated", "Normal"),
                           ylabels))


       par(mar=c(5.1, max(4.1,max(nchar(ylabels))/1.8) ,4.1 ,2.1))

       plot_colors <- c("#458B74","#8B0000")
       barplot(test,
               col = plot_colors,
               las=2,
               beside = TRUE,
               legend = T,
               args.legend = list(x="topright", cex=.75),
               xlim = c(0,110),
               horiz=T)

       abline (v=seq(0,80, 20))

这是图表,这是:

这是我想要的图表,但我想直接从制表符分隔的输出文件创建它,看起来像这样(没有标题):

   sampleA  7
   sampleB  0
   sampleC  53
   sampleD  0
   sampleE  28
   sampleF  0
   sampleG  0
   sampleXY 0
   sampleXYZ    12

【问题讨论】:

    标签: r dynamic graph bar-chart


    【解决方案1】:

    这样的事情可能会奏效

    table=read.table(file="testGraphData.txt")
    names=table[,1]
    values=table[,2]
    barplot(values, names=names)
    

    table=read.table(file="testGraphData.txt")
    barplot(table[,2], names=table[,1])
    

    【讨论】:

    • 我还想绘制正常值以及如何添加该部分的任何想法?感谢您的即时回复。
    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2022-08-03
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    相关资源
    最近更新 更多