data visualization with ggplot2: set axes limits for each graphs generated with geom_facet

1,211 views
Skip to first unread message

chaimae eljaouhari

unread,
Jun 2, 2017, 5:43:49 AM6/2/17
to ggplot2

I develop visulization tool with ggplot2. my dataframe is a table with 6 columns. In my dataframe, i have a column "Chrom_length" which containe the xlim value of every graph generated with facet_wrap.

when i use xlim or scale_x_continuous i have error (length(lims) == 2 is not TRUE or Error in zero_range(range) : x must be length 1 or 2.), and when i use expand_limits, it return the maximum of Chrom_length column, so i want that it return the value correspond for each Chromosome .

my dataframe is

Dataset Chromosome  Chrom_length    Coordinate  Nbr_reads   Polarity
dataset_1   FBtr0070001 72  17  1   F
dataset_1   FBtr0070604 72  1   1   F
dataset_1   FBtr0070911 73  1   1   R
dataset_1   FBtr0070911 73  4   1   F
dataset_1   FBtr0070911 73  7   1   F
dataset_1   FBtr0071581 71  1   2   F
dataset_1   FBtr0071581 71  4   1   R
dataset_1   FBtr0071630 71  1   1   F
dataset_1   FBtr0071630 71  3   1   F
dataset_1   FBtr0071736 72  3   6   F
dataset_1   FBtr0071737 72  3   1   F
dataset_1   FBtr0071962 82  55  1   R
dataset_1   FBtr0072445 72  1   2   F

My R script :

# Table is the data frame
library("ggplot2")
library("gridExtra")

theme_set(theme_bw())
#Table=read.delim(your_input, header=T, row.names=NULL)
Table <- within(Table, Nbr_reads[Polarity=="R"] <- (Nbr_reads[Polarity=="R"]*-1)) 
p <- ggplot(Table, aes(x=Coordinate, y=Nbr_reads, label=Nbr_reads, colour=Polarity)) +
#  xlim(0,Table[Chromosome,"Chrom_length"])+
  geom_segment(aes(y = 0, 
                   x = Coordinate, 
                   yend = Nbr_reads, 
                   xend = Coordinate)
               ) +
  facet_wrap(Dataset~Chromosome, scales="free")+
  expand_limits(x= c(0, unique(Table[Table$Chromosome,"Chrom_length"])))+
  geom_hline(yintercept=0)

plot.list <- by(data     = Table,
                INDICES  = c(Table$Chromosome),
                simplify = TRUE,
                FUN      = function(x) {
                  p %+% x 
                })
multi.plot <- marrangeGrob(grobs = plot.list,nrow  = 4, ncol = 1, top=NULL);
ggsave(file="test.pdf", plot=multi.plot, height=11.69, width=8)

examples:

the xlim of "FBtr0070911" is : (0,73)
the xlim of "FBtr0071581" is:  (0,71)
the xlim of "FBtr0071962" is: (0,82)
the xlim of "FBtr0072445" is: (0,72)

can you help me for resolving this problem?

Thank you.

eipi10

unread,
Jun 17, 2017, 1:07:53 AM6/17/17
to ggplot2
To set the axis ranges separately for each facet I think you actually need to create a separate plot for what was formerly each facet (which gives you direct control over the x-range for each facet) and then lay the plots out together as if they had been generated using facet_wrap. Here and here are a couple of examples from answers I've written on Stack Overflow and I'm sure there are other examples on SO as well.
Reply all
Reply to author
Forward
0 new messages