1

I have this data called d. I am using the code below to plot the data, but the range in X axis is very high. How can I make the X axis labels fit with the plot? I also want to plot them in order, starting from chr1 to chr16. Thank you for your help.

    d<- structure(list(chr = c("chr10", "chr10", "chr10", "chr11", "chr11", 
"chr11", "chr12", "chr12", "chr12", "chr13", "chr13", "chr13", 
"chr14", "chr14", "chr14", "chr15", "chr15", "chr15", "chr16", 
"chr16", "chr16", "chr1", "chr1", "chr1", "chr2", "chr2", "chr2", 
"chr3", "chr3", "chr3", "chr4", "chr4", "chr4", "chr5", "chr5", 
"chr5", "chr6", "chr6", "chr6", "chr7", "chr7", "chr7", "chr8", 
"chr8", "chr8", "chr9", "chr9", "chr9"), start = c(6, 14, 19, 
3, 10, 16, 60, 73, 80, 4, 11, 27, 67, 75, 81, 2, 7, 15, 13, 99, 
142, 1, 8, 21, 49, 53, 78, 92, 121, 165, 35, 42, 47, 5, 9, 17, 
12, 20, 34, 44, 51, 56, 61, 66, 94, 18, 31, 37), stop = c(6, 
14, 19, 3, 10, 16, 60, 73, 80, 4, 11, 27, 67, 75, 81, 2, 7, 15, 
13, 99, 142, 1, 8, 21, 49, 53, 78, 92, 121, 165, 35, 42, 47, 
5, 9, 17, 12, 20, 34, 44, 51, 56, 61, 66, 94, 18, 31, 37), density = c(10, 
4, 1, 13, 1, 1, 18, 48, 526, 3, 1, 1, 13, 74, 177, 1, 3, 5, 9432, 
3, 5, 1, 32, 1, 60, 4, 3, 2, 1, 10, 4, 10, 6, 6, 2, 3, 5, 65220, 
11136, 1, 25, 36, 5, 6, 1, 4, 7, 11)), .Names = c("chr", "start", 
"stop", "density"), row.names = c(NA, -48L), class = "data.frame")

my code:

library(ggplot2)
  ggplot(d, aes(start, density)) +
  geom_line(alpha = 0.9) +
  facet_grid(. ~ chr) +
  labs(title = "Density profiles along the chromosomes",
       x = "Coordinate, bp",
       y = "Density") +
  theme_bw()
MAPK
  • 5,635
  • 4
  • 37
  • 88
  • 2
    For your second question: `df$chr` should be a factor, see [here](https://stackoverflow.com/questions/14262497/fixing-the-order-of-facets-in-ggplot). Not entirely sure about your first question, are you looking for `... + facet_grid(. ~ chr, scales = "free_x") + ...` ? – markus Dec 19 '17 at 19:01
  • @markus I want to minimize the overcrowding of values in X-legend. – MAPK Dec 19 '17 at 19:05
  • Try this: `d$chr <- factor(d$chr, levels = c('chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6', 'chr7', 'chr8', 'chr9', 'chr10', 'chr11', 'chr12', 'chr13', 'chr14', 'chr15', 'chr16'))` The levels are important. – markus Dec 19 '17 at 19:12
  • @markus Thank you so much, that solved the problem of faceting order. – MAPK Dec 19 '17 at 19:17
  • You're welcome. I don't see a clear solution to your x-axis problem, since the overlay depends on how big you draw the graphic. You could add `+ scale_x_continuous(breaks = c(25, 75, 125))` to your plot (or any other break values, of course). – markus Dec 19 '17 at 19:34
  • @markus How do I put break for each chr range for X axis? – MAPK Dec 19 '17 at 19:38
  • I don't know. Maybe [this](https://stackoverflow.com/questions/18819333/is-it-yet-possible-to-have-different-axis-breaks-limits-for-individual-facets) helps you out. For your problem it could be helpful to rotate the x-axis labels. Add `+ theme(axis.text.x = element_text(angle = -90, vjust = 1, hjust = 0))` after the call to `theme_bw()`. – markus Dec 19 '17 at 20:01

1 Answers1

0

I don't think it's possible to set axis ticks individually for each facet. You may be better off arranging the facets in a grid.

d$chr <- factor(d$chr, levels = c('chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6', 'chr7', 'chr8',
                                  'chr9', 'chr10', 'chr11', 'chr12', 'chr13', 'chr14', 'chr15', 'chr16'))

library(ggplot2)
ggplot(d, aes(start, density)) +
  geom_line(alpha = 0.9) +
  facet_wrap(~ chr, nrow = 4, scales = "free_x") +
  labs(title = "Density profiles along the chromosomes",
       x = "Coordinate, bp",
       y = "Density") +
  theme_bw()

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104