2016-01-29 102 views
1

我在使用ggplot2分面圖時過寬的面板標籤時遇到問題。用ggplot調整面板標籤寬度

這裏是我用來生成劇情代碼:

png(paste("/directory/", hgnc_symbol, "_", curr_gene, ".png", sep=""), 
    width=4, height=3, units="in", pointsize=1, res=300) 
    print({barplot <- 
    ggplot(curr_data, aes(x = condition, y = tpm, fill=condition)) + 
    geom_boxplot(outlier.colour=NA, lwd=0.2, color="grey18") + 
    stat_boxplot(geom ='errorbar', color="grey18") + 
    geom_jitter(size=0.8) + 
    facet_wrap(~target_id) + 
    guides(fill=FALSE) + 
    theme_bw() + 
    labs(title=paste(hgnc_symbol, "_", curr_gene, sep="")) + 
    labs(x="condition") + labs(y="TPM") + 
    theme(text = element_text(size=5), strip.background=element_rect(size = 1), 
     axis.text.x = element_text(angle = 90, hjust = 1, size=4.5))}) 
    dev.off() 

情節找出來是這樣的:

enter image description here

正如你所看到的,面板的背景標籤如此之廣,以至於這些情節本身幾乎不可見。圖上繪製的點也比我預期的要大得多。

奇怪的是,我用這個完全相同的代碼生成此下面的圖形(看起來好)短短數天前:

enter image description here

是什麼導致了這種差異,我怎麼能解決問題?

+0

當您提供一些示例數據時,您可以獲得更好的結果。但是,我認爲問題是ggplot用於繪圖和標籤的不同尺度。你說你使用完全相同的代碼得到了不同的結果。我敢打賭,你將圖表保存在不同的維度下。試着增加你的寬度和高度,看看你的結果是否接近你的第二個更理想的圖。 – oshun

回答

0

在ggplot中,text is defined in pts,它們是絕對測量單位。繪圖面板的大小是相對的,並根據您在保存繪圖時指定的尺寸進行縮放。如果尺寸很小,那麼文本將相對於面板區域較大。如果尺寸很大,那麼相對於面板區域,文字會很小。參看下面的例子:

ggplot(diamonds, aes(x = x, y =y)) + geom_point() + facet_wrap(~ clarity) 
ggsave("filepath//4x3.png", width=4, height = 3) 
ggsave("filepath//8x6.png", width=8, height = 6) 

在圖中的4×3: 4x3.png

在圖中的8×6: enter image description here

可以編輯grobs對情節尺寸施加更精細的控制(example)。

+0

謝謝@oshun;我很欣賞這個解釋。如您在原始評論中所建議的那樣調整尺寸,解決了問題。 – mshum