Multiple ggplots errors

Hello. I need your help for ggplots.
I am trying to add multiple ggplots in the same "sheet". The issue that Iam facing is that at the end of plots (both Error_gg and correct_gg) I have this error (Error: Aesthetics must be either length 1 or the same as the data (512): colour)

I wrote the following:

library(matrixStats)
library(ggplot2)
library(ggpubr)
library(grid)


dedomena_Error <-read.table("/home/kgee/Desktop/file1")
dedomena_correct <- read.table("/home/kgee/Desktop/strand_bias/file2")



correct_g <- ggplot(data=dedomena,aes(x=dedomena_correct$V3,y=dedomena_correct$V2))
Error_g <- ggplot(data=dedomena_Error,aes(x=dedomena_Error$V5,y=dedomena_Error$V4))


Error_gg <-g + geom_point(aes(colour =dedomena_Error$V5))+ geom_line(aes(dedomena_Error$V4))+ggtitle("Strand bias plot")+xlab("genome length")+ylab("genes in strand oriantation") + theme_classic()
correct_gg <-g + geom_point(aes(colour =dedomena_correct$V2))+ geom_line(aes(dedomena_correct$V3))+ggtitle("Strand bias plot")+xlab("genome length")+ylab("genes in strand oriantation") + theme_classic()


Error_gg
correct_gg

#(Error_gg +correct_gg) I dont know if its the proper way to add two plots together multiple_gg 

My input files:

head(file1)
V1 V2  V3 V4   V5
1  - 966 -1  966
2  - 537 -2 1503
3  - 369 -3 1872
4  - 573 -4 2445
5  - 603 -5 3048
6  - 381 -6 3429

head(file2)
 V1 V2 V3
 1 -1 1083
 2 -2 2172
 3 -1 2742
 4 -2 2988
 5 -3 3147
 6 -4 4218

Thanks in advance!!!

You need to pay more attention to details, you are making a lot of syntax errors and using undefined variable names, for example:

You haven't defined dedomena, you just have defined dedomena_Error and dedomena_correct. Also, you are using invalid syntax to define the aesthetics, no need for this part dedomena_correct$.

You haven't defined g so there is nothing to add to.

See this example correcting your syntax errors (BTW notice how the example is self-contained you should do the same next time)

library(ggplot2)

dedomena_correct <- data.frame(
          V1 = c(1, 2, 3, 4, 5, 6),
          V2 = c(-1, -2, -1, -2, -3, -4),
          V3 = c(1083, 2172, 2742, 2988, 3147, 4218)
)

ggplot(data = dedomena_correct, aes(x = V3, y = V2)) +
    geom_point(aes(colour = V2))+
    geom_line(aes(V3)) + 
    ggtitle("Strand bias plot") + 
    xlab("genome length") + 
    ylab("genes in strand oriantation") +
    theme_classic()

Created on 2020-02-11 by the reprex package (v0.3.0.9001)

2 Likes

First of all, thanks a lot for the reply. I used the example you suggested me and works!!! I need some other specifications, if you don't mind. Reading your second correction, if I have more than more than 1 ggplots on my code how should I difined them.? "name<- ggplot(data = dedomena_correct, aes(x = V3, y = V2)) etc ? Also how to combine multiple ggplots if they have the name ggplot??

You can totally save them as individual variables, like you suggest. But I really like storing them together as a named list, b/c I think it keeps my environment cleaner.

my_plots <- list()

my_plots[["my pretty line plot"]] <- ggplot(data = dedomena_correct, aes(x = V3, y = V2)) + geom_line()

my_plots[["the ugly dot plot"]] <- ggplot(data = dedomena_correct, aes(x = V3, y = V2)) + geom_point()

If you want to combine plots into a single image, my favorite package is cowplot

cowplot::plot_grid(plotlist = my_plots)

But there are other packages like egg and patchwork that do the same thing.

thanks a lot for the suggestions. I used the egg as the cowplot requires the newest version of R. and I got almost what I want. So I need to practice with my new toy :slightly_smiling_face:. I know that I have a lot to do for improving my coding appearance and I'm on this way. Once again thank you very much for your help!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.