显示ggplot2标题而不预留空间 [英] show ggplot2 title without reserving space for it

查看:191
本文介绍了显示ggplot2标题而不预留空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为一些ggplot2图表设置标题,同时留下一些没有标题的标题。不幸的是,当设置标题时,y轴和绘图缩小(参见右图)。我需要在不改变Y轴大小的情况下绘制标题,以便标题图表与其他标题(如中间图表)相同。

I am trying to set titles to some ggplot2 charts, while leaving some without titles. Unfortunately, when title is set, the y axis and the plot shrink (see the plot on the right). I need to plot the title without changing the Y axis size, so that titled charts are on the same scale with the others (as in the middle plot).

grid.arrange(
  (ggplot(mtcars, aes(mpg, hp)) + geom_point()),
  (ggplot(mtcars, aes(mpg, hp)) + geom_point() + 
    geom_text(aes(22.5, 340, label="fake title",  vjust = 1, hjust = .5, show_guide = FALSE))),
  (ggplot(mtcars, aes(mpg, hp)) + geom_point() +
    labs(title="real title")),
  ncol=3)

我不能在其他地块上使用虚假的空字符串标题,因为我的空间很短。
我可以使用 geom_text()方法,如果有人能告诉我如何使它看起来更少乱码。
那么,如何去除剧情上方标题的任何保留空间,同时仍然在剧情区顶部显示剧情标题?后者用主题(plot.title = element_text(vjust = -1))完成。)

I cannot use fake empty-string titles on the other plots, because I am short on space. I could use the geom_text() method, if anyone can tell me how to make it look less garbled. So, how do I removing any reserved space for the title above the plot, while still showing the plot title on and at the top of the plot area? The latter is done with theme(plot.title = element_text(vjust=-1)).)

推荐答案

您应该为此使用注释。如果您使用 geom_text ,它将打印出与您的数据中的行数一样多的标签,因此您的问题中标签看起来很糟糕。一个痛苦的解决办法是创建一个1行数据框用作 geom_text 图层的数据。但是, annotate 适用于这类事情,所以您不需要解决方法。例如:

You should use annotate for this. If you use geom_text, it will print as many labels as there are rows in your data, hence the poor-looking overplotted label in your question. A painful work-around is to create a 1-row data frame to use as the data for the geom_text layer. However, annotate is intended for this sort of thing so you don't need a work-around. Something like:

 annotate(geom = "text", x = 22.5, y = 340, label="fake title")

是很好的做法。注释也可用于将单条水平线或垂直线添加到绘图,或通过在其周围绘制矩形来突出显示某个区域。

is good practice. Annotate is also useful for adding single horizontal or vertical lines to a plot, or to highlight a region by drawing a rectangle around it.

这篇关于显示ggplot2标题而不预留空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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