2017-12-20 6 views
0

내 데이터 플롯에이 코드가 있습니다 d. 나는 아래의 코드를 가지고 멋진 플롯을 얻었는데, X 축은 함께 묶여있다. 이 플롯에서 멋진 축 값을 분리하려면 어떻게해야합니까?facet_grid를 사용하여 ggplot에 X 축 레이블을 맞출 수 있습니까?

내 데이터 :

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") 

내 코드 :

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)) + 
    # ggplot(d, aes(start, density)) + 
    geom_line(alpha = 0.9) + 
    # facet_grid(. ~ chr) + 
    facet_grid(. ~ chr, scales = "free_x") + 

    # limits from the data frame 
    scale_x_continuous(expand = c(0, 0), 
        breaks = function(x) x) + 

    labs(title = "Density profiles along the chromosomes", 
     x = "Coordinate, bp", 
     y = "Density") + 
    theme(plot.title = element_text(hjust = 0.5), 

    # align x-axis labels inwards for easier reading 
    axis.text.x = element_text(hjust = c(-0.1, 4))) 

    filename <- "plot.tiff" 
    ggsave(file = paste0(plot.dir,filename), dpi = 300, width = 8, 
    height = 6, units = "in") 

답변

1
(ggplot(d, aes(start, density)) + 
    geom_line(alpha = 0.9) + 
    scale_x_continuous(name = "Corodinate, bp", expand = c(0, 0), breaks = function(x) x) + 
    scale_y_continuous(labels=scales::comma) + 
    facet_grid(. ~ chr, scales = "free_x") + 
    labs(title = "Density profiles along the chromosomes") + 
    theme(plot.title = element_text(hjust = 0.5)) + 
    theme(panel.spacing.x=unit(12, "pt")) + 
    theme(axis.text.x = element_text(size=6, hjust = c(0, 1))) -> gg) 

filename <- "plot.tiff" 
ggsave(file = filename, plot = gg, dpi = 300, width = 8, height = 6, units = "in") 

enter image description here