-1

I am trying to plot three data sets that share an x axis. some of the data sets, however, have missing data and are thus of different length. I can plot them fine individually but when I try to facet them all together I get an error that the data sets contain different numbers of rows. This error only occurs when I facet the plot (which is necessary).

Any suggestions for how I could get the facet plot to accept data sets with different numbers of rows?

The code i've been using is:

ggplot()+
 geom_line(data=x,aes(x=x$BIN_START,y=x$TajimaD),size=0.6,alpha=0.65,colour="skyblue1")+
geom_line(data=y,aes(x=y$BIN_START,y=y$TajimaD),size=0.3,alpha=0.85,colour="greenyellow")+
 geom_line(data=z,aes(x=z$BIN_START,y=z$TajimaD),size=0.25,alpha=0.95,colour="black")+
  scale_x_continuous()+
  facet_grid(rows=vars(x$CHROM))+
  theme_classic()+
  ylab("TajimaD") +
  xlab("Location (bp)") 

As was suggested in a comment I have now moved all the data into a single file and added a column to indicate the population the data is from. I am still getting a similar error message: "replacement has 22588 rows, data has 7537"

ggplot()+
  geom_line(data=x,aes(x=a$BIN_START,y=a$TajimaD,color=a$Population),size=0.6,alpha=0.65)+
  scale_x_continuous()+
  facet_grid(rows=vars(a$CHROM))+
  theme_classic()+
  ylab("TajimaD") +
  xlab("Location (bp)")
Greg T.D.
  • 49
  • 5
  • 4
    Most likely it will help if you reshape the data to one long dataframe and use one `geom_line` call. If you provide a data sample with `dput` we can better debug – Calum You Jul 10 '19 at 21:03
  • 1
    [Don't use `$` in your `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – camille Jul 10 '19 at 21:10
  • @CalumYou I have done as you suggested. thoughts? – Greg T.D. Jul 10 '19 at 21:26

1 Answers1

0

On your second attempt you're using x as data but then use a$BIN_START, etc. It's very likely that x and a have a different number of rows, and hence the error. I suggest removing the <dataset_name>$ alltogether in all your aes() calls when you use ggplot2. When you say data = x, you only need to write aes(x=BIN_START,y=TajimaD,color=Population) (i.e. no need for x$).

Felipe Gerard
  • 1,552
  • 13
  • 23