简体   繁体   English

用ggplot2动态绘图

[英]dynamically plotting with ggplot2

i'm new to ggplot and i'm trying to automatically plot multiple subset of the data to different pdf files , but i'm encoutering an error and need your help. 我是ggplot的新手,我正在尝试将数据的多个子集自动绘制到不同的pdf文件中,但是我遇到了错误,需要您的帮助。

that's my code : 那是我的代码:

library(ggplot2)
t=read.table("../All.txt",stringsAsFactors=FALSE)
names( t ) <- c("A","C","G","T","(A-T)/(A+T)","(G-C)/(G+T)","(A+T)/(G+C)","accession","Phylum","Order","Class")
    phy=unique(c(t$Phylum))
    for (x in phy){ 
    if(x=="???:???")
    {
        x="unknown"
    }
    pdf(paste(x,".pdf") , width=25, height=15)
    test<-subset(t, Phylum==x)
    dat <- melt(test, measure=c("A", "C" , "G" , "T" , "(A-T)/(A+T)", "(G-C)/(G+T)","(A+T)/(G+C)"))
    ggplot(dat, aes(Class,value , color=variable))  + geom_boxplot() +geom_jitter()   +  facet_grid(variable~., scales="free_y")
    }

the error is : 错误是:

argument implies differing number of row: 0,1

how can i fix this error? 我该如何解决此错误? thanks for your help 谢谢你的帮助

I deleted my original answer, and started a new: 我删除了原始答案,然后开始了新的回答:

t <- structure(list(A = 0.286945, C = 0.322006, G = "0.1473610.2436880.081520-0.4466031.130529NC_000846", T = "Chordata", `(A-T)/(A+T)` = "Rheiformes", `(G-C)/(G+T)` = "Aves", `(A+T)/(G+C)` = 0.39562, accession = "0.1334170.0917400.3792240.021160-0.0884933.441356NC_000857", Phylum = "Arthropoda", Order = "Diptera", Class = "Insecta"), .Names = c("A", "C", "G", "T", "(A-T)/(A+T)", "(G-C)/(G+T)", "(A+T)/(G+C)", "accession", "Phylum", "Order", "Class"), class = "data.frame", row.names = c(NA, -1L))

If I run your code, it runs without a warning message, though no plot was saved as did not specified dev.off at the end of the loop. 如果我运行您的代码,尽管在循环结束时未指定dev.off ,但未保存任何图,但它没有警告消息。 You could upload your data file to eg. 您可以将数据文件上传到例如。 pastebin . pastebin


UPDATE: based on demo data file 更新:基于演示数据文件

Thanks for uploading a sample dataset! 感谢您上传样本数据集! I run a modified version of your code (to be able to save the plots in pdf) which run without error/warning: 我运行了代码的修改版本(以便能够将图保存为pdf),并且运行时没有错误/警告:

t=read.table("txt-part.txt", stringsAsFactors=FALSE)
names( t ) <- c("A","C","G","T","(A-T)/(A+T)","(G-C)/(G+T)","(A+T)/(G+C)","accession","Phylum","Order","Class")
phy=unique(t$Phylum)

for (x in phy){ 
    if(x != "???:???") {
    test<-subset(t, Phylum==x)
    dat <- melt(test, measure=c("A", "C" , "G" , "T" , "(A-T)/(A+T)", "(G-C)/(G+T)","(A+T)/(G+C)"))
    p <- ggplot(dat, aes(Class,value , color=variable))  + geom_boxplot() +geom_jitter()   +  facet_grid(variable~., scales="free_y")
    ggsave(paste(x,".pdf"), p, width=25, height=15)
    }}

I have got 2 pdf files as expected on your sample data set. 我在您的样本数据集中得到了2个pdf文件。 If it does not run on the whole data set, I have no idea of the problem without checking the original data file. 如果它不能在整个数据集上运行,那么我不检查原始数据文件就不会知道这个问题。 Maybe others do! 也许别人做! :) :)

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

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