Reputation: 6874
I have a load of genomic data (dput way too large) Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 7454 obs. of 3 variables:
$ chr : num 1 1 1 1 1 1 1 1 1 1 ...
$ leftPos: num 480000 600000 2520000 2760000 2880000 3000000 3120000 3480000 3600000 4440000 ...
$ Means : num 45.2 58.3 10.7 81.2 16 ...
- attr(*, "vars")=List of 1
..$ : symbol chr
- attr(*, "labels")='data.frame': 22 obs. of 1 variable:
..$ chr: Factor w/ 24 levels "chr1","chr10",..: 1 2 3 4 5 6 7 8 9 10 ...
..- attr(*, "vars")=List of 1
.. ..$ : symbol chr
- attr(*, "indices")=List of 22
..$ : int 0 1 2 3 4 5 6 7 8 9 ...
..$ : int 559 560 561 562 563 564 565 566 567 568 ...
..$ : int 908 909 910 911 912 913 914 915 916 917 ...
..$ : int 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 ...
..$ : int 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 ...
..$ : int 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 ...
..$ : int 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 ...
..$ : int 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 ...
..$ : int 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 ...
..$ : int 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 ...
..$ : int 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 ...
..$ : int 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 ...
..$ : int 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 ...
..$ : int 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 ...
..$ : int 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 ...
..$ : int 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 ...
..$ : int 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 ...
..$ : int 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 ...
..$ : int 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 ...
..$ : int 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 ...
..$ : int 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 ...
..$ : int 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 ...
- attr(*, "group_sizes")= int 559 349 383 370 283 229 177 173 140 222 ...
- attr(*, "biggest_group_size")= int 682
I would like to plot this on a facet plot but limit the x ax-s to the maximum of the leftPos for each chr. At the moment the facet is plotted for each chr with equal width. When I use scales="free_x" the facet just stretches the plot to fill a pre-defined width. Is it possible to have different width facets?
The code I'm using so far:
ggplot(Zoutliers1,aes(x = leftPos,
y = as.numeric(Means),
group = chr,
xend = leftPos,
yend=0))+
geom_bar(stat="identity",fill = "red", size = 1, colour = "red")+
geom_line()+
geom_segment(linetype= 1, colour = "#919191")+
ggtitle(TBBName)+
ylim(-50,480)+
facet_wrap(~ chr,nrow = 1)+
geom_hline(yintercept = UL1)+
geom_hline(yintercept = LL1)+
theme(panel.margin = unit(0.1, "lines"))+
theme(axis.text.x = element_blank())+
theme(panel.border = element_rect(fill=NA,color="darkred", size=0.5,
linetype="dashed"))
The plot I'm getting:
Upvotes: 2
Views: 640
Reputation: 13139
You need to set the 'space' parameter to 'free', which can only be done in facet_grid. Here is a demonstration with sample data.
library(gridExtra)
library(ggplot2)
#creating some sample data
set.seed(10001)
dat <-data.frame(chr=1:6,leftPos=seq(100,1000,length.out=6))
dat2 <- dat[sample(1:nrow(dat),1000,T),]
dat2$x <- rnorm(nrow(dat2))*dat2$chr
#basic plot
p1 <- ggplot(dat2, aes(x=x)) +
geom_histogram()
#different scales
p_scales <- p1 + facet_grid(.~chr, scales="free_x") + labs(title="free x, default space")
p_space_scales <- p1 + facet_grid(.~chr, scales = "free_x",space="free") + labs(title="free x and free space")
grid.arrange(p_scales,p_space_scales)
Upvotes: 5