【问题标题】:Adjust width of panel labels on ggplot with faceting使用刻面调整 ggplot 上面板标签的宽度
【发布时间】:2016-01-29 20:50:24
【问题描述】:

我的 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()

情节是这样的:

如您所见,面板标签的背景非常宽,以至于图本身几乎看不到。图表上绘制的点也比我预期的要大得多。

奇怪的是,就在几天前,我使用了同样的代码来生成下面的图(看起来不错):

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

【问题讨论】:

  • 当您提供一些样本数据时,您可以获得更好的结果。但是,我认为问题在于 ggplot 用于绘图和标签的不同比例。你说你用完全相同的代码得到了不同的结果。我敢打赌您将图表保存在不同的维度下。尝试增加你的宽度和高度,看看你的结果是否更接近你的第二个更理想的图表。

标签: r plot graphics ggplot2


【解决方案1】:

在 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:

8 x 6 的情节:

您可以编辑 grobs 以更好地控制绘图尺寸 (example)。

【讨论】:

  • 谢谢@oshun;我非常感谢您的解释。正如您在原始评论中建议的那样调整尺寸解决了问题。
猜你喜欢
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2021-12-16
  • 2012-10-05
  • 2018-05-20
  • 1970-01-01
相关资源
最近更新 更多