简体   繁体   中英

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.

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. You could upload your data file to eg. 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:

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. 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! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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