将y轴标题放在图的左上角

编程入门 行业动态 更新时间:2024-10-24 12:26:49
本文介绍了将y轴标题放在图的左上角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在用R中的ggplot2绘制一幅情节,我想让y轴的标题出现在情节的左上角。考虑以下代码的默认行为:

require(ggplot2) xy = data.frame (x = 1:10,y = 10:1) ggplot(data = xy)+ geom_point(aes(x = x,y = y))+ ylab(非常长的标签)

这产生了下面的图:

我想移动并旋转文本非常长的标签。我可以使用 theme()函数来做到这一点:

ggplot (data = xy)+ geom_point(aes(x = x,y = y))+ ylab(very long label)+ theme(axis.title.y = element_text (angle = 0,vjust = 1.1,hjust = 10))

p>

您可以看到我要去的位置,但边距不正确 - 左边距太大,因为该空间是为旋转的标签保留的,而顶部边距对于文本来说太小。

如何告诉ggplot我希望在那个位置没有旋转的情况下使用y轴标题,并让它为它保留适当的空间?

解决方案

把它放在主标题中:

ggplot (data = xy)+ geom_point(aes(x = x,y = y))+ ggtitle(very long label)+ theme(plot.titl e = element_text(hjust = 0))

如果你喜欢,使用负值 hjust 值,尽管如果你太过分了,标签会被裁剪掉。在这种情况下,您可以尝试使用 plot.margin :

ggplot(data = xy)+ geom_point(aes(x = x,y = y))+ ggtitle(very long label)+ theme(plot.title = element_text hjust = -0.3), plot.margin = rep(grid :: unit(0.75,in),4))

所以很显然,这很难为图形添加实际标题。您可以使用类似以下方式手动注释:

grid.text(Actual Title,y = unit(0.95,npc ))

反之亦然,使用 grid.text code>为y标签。

I'm drawing a plot with ggplot2 in R and I'd like the title for the y axis to appear in the top left corner of the plot. Consider the following code for the default behaviour:

require(ggplot2) xy = data.frame(x=1:10, y=10:1) ggplot(data = xy) + geom_point(aes(x = x, y = y)) + ylab("very long label")

This produces the following graph:

I would like to move and rotate the text "very long label". I can do this somewhat using the theme() function:

ggplot(data = xy) + geom_point(aes(x = x, y = y)) + ylab("very long label") + theme(axis.title.y = element_text(angle = 0, vjust = 1.1, hjust = 10))

Which gives me this:

You can see where I'm going with this, but the margins are incorrect -- the left margin is too large because the space is reserved for a rotated label and the top margin is too small for the text.

How can I tell ggplot that I want the y axis title at that position without rotation and have it reserve the appropriate space for it?

解决方案

Put it in the main plot title:

ggplot(data = xy) + geom_point(aes(x = x, y = y)) + ggtitle("very long label") + theme(plot.title = element_text(hjust = 0))

You can shove it slightly more to the left if you like using negative hjust values, although if you go too far the label will be clipped. In that case you might try playing with the plot.margin:

ggplot(data = xy) + geom_point(aes(x = x, y = y)) + ggtitle("very long label") + theme(plot.title = element_text(hjust = -0.3), plot.margin = rep(grid::unit(0.75,"in"),4))

So obviously this makes it difficult to add an actual title to the graph. You can always annotate manually using something like:

grid.text("Actual Title",y = unit(0.95,"npc"))

Or, vice-versa, use grid.text for the y label.

更多推荐

将y轴标题放在图的左上角

本文发布于:2023-07-18 04:49:38,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:放在   左上角   标题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!