使用刻面调整ggplot上面板标签的宽度

时间:2016-01-29 20:50:24

标签: r plot graphics ggplot2

我在ggplot2刻面图上遇到过宽的面板标签时遇到问题。

以下是我用于生成图表的代码:

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

造成这种差异的原因是什么,以及如何解决问题?

1 个答案:

答案 0 :(得分: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 x 3的情节: 4x3.png

8 x 6的情节: enter image description here

您可以编辑凹凸以对绘图尺寸(example)施加更精细的控制。