如何在不改变绘图宽度的情况下使用ggplot2在R中添加可变大小的y轴标签? [英] How can I add variable size y-axis labels in R with ggplot2 without changing the plot width?

查看:189
本文介绍了如何在不改变绘图宽度的情况下使用ggplot2在R中添加可变大小的y轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中使用了ggplot2绘制的图,我想在y轴上添加一个水平文本标签。但是,根据我的文本的长度,R会相应地压缩我的绘图以创建固定宽度的图像。不过,我需要的地块长度相同,并且无论文字宽度如何,都有相同的起始和停止位置(边距)。我尝试通过覆盖默认的ggplot2主题元素如下所示:

  library(ggplot2)
png(filename =sample.png,width = 5600,height = 70)
plot.data< - data.frame(start.points = c(my_start),end.points = c(my_stop))
p < - ggplot数据)
p + geom_rect(aes(xmin = start.points,xmax = end.points,ymin = 0,ymax = 1),fill =red)+ theme_bw()+ ylab(sample_title)+
theme(axis.title.y = element_text(size = 30,color =black,angle = 0),axis.text = element_blank(),legend.key = element_blank(),axis.ticks = element_blank (),plot.margin = unit(c(0.1,0.1,0.1,12),lines))
dev.off()

所以这是一个很好的情节,但根据我的轴标签,整个情节会被拉伸或压缩。有人可以帮助我找到一种方法来保持绘图宽度和边距静止,同时更改轴标签文本?如果轴标签文本太长,并且它延伸到左侧图像边界之外,那么没关系,只要情节没有改变即可。



感谢您的帮助!在这种情况下,我一直对此感到震惊。

解决方案

更改边距在这种情况下无助于解决问题,因为除了您的轴标签外,他们只需添加固定的空间。您可以将您的轴标签贴到您预期的最大长度以保持尺寸相同,但这不是最优雅的解决方案,但我不认为 element_text 可以在那一刻(也许有人可以纠正我)。无论如何,你可以尝试其中的一种:

  ylab(sprintf(%40s,sample_title))

(使用空格将轴标题填充为40个字符 - 尽管总宽度仍会因您的字体而异) p>

  ylab(粘贴(sprintf(%40s,),\\\
sample_title\\\

(在轴标题上方添加一行40个空格并在下面显示一条空行 - 应始终给出与只要轴标题不长于此)


I have a plot I made with ggplot2 in R that I would like to add a horizontal text label to the y-axis. However, depending on the length of my text, R compresses my plot accordingly to create a fixed width image. However, I need the plots to be identical length and have identical starting and stopping positions (margins) no matter the text width.

I tried changing the plot margins by overwriting the default ggplot2 theme elements like so:

library(ggplot2)
png(filename="sample.png", width=5600, height=70)
plot.data <- data.frame(start.points=c(my_start),end.points=c(my_stop))
p <- ggplot(plot.data)
p + geom_rect(aes(xmin=start.points, xmax=end.points, ymin=0, ymax=1), fill="red") + theme_bw() + ylab("sample_title") + 
theme(axis.title.y = element_text(size = 30, colour = "black", angle = 0), axis.text = element_blank(), legend.key = element_blank(), axis.ticks = element_blank(), plot.margin = unit(c(0.1, 0.1, 0.1, 12), "lines"))
dev.off()

So this makes a nice plot, but depending on my axis label the entire plot is stretched or compressed. Can someone please help me find a way to hold the plot width and margins static while changing the axis label text? If the axis label text is too long and it extends outside of the left-hand image border on into the plot itself, that is ok, as long as the plot doesn't change.

Thanks for any help! I've been banging my head on this for way too long.

解决方案

Changing the margins doesn't help in this case because they just add fixed space in addition to your axis label. You could pad your axis label to your expected maximum length to keep the dimensions the same, it's not the most elegant solution but I don't think element_text can do a fixed width at the moment (maybe someone can correct me on this). Anyways, you could try one of these:

ylab(sprintf("%40s", "sample_title"))

(padding the axis title to 40 characters with spaces - although total width will still vary depending on your font)

ylab(paste(sprintf("%40s", ""), "\nsample_title\n")

(adding a line of 40 spaces above your axis title and an empty line below - should always give you the same width as long as the axis title isn't longer than that)

这篇关于如何在不改变绘图宽度的情况下使用ggplot2在R中添加可变大小的y轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆