多行ggplot标题具有不同的字体大小,脸部等 [英] Multi-line ggplot Title With Different Font Size, Face, etc

查看:204
本文介绍了多行ggplot标题具有不同的字体大小,脸部等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了SO和其他网站,但无法找到解决我的问题的方法,即 ggtitle()。我知道还有其他的例子,但我没有能够成功地将它们直接应用于我的问题。

I have scoured SO and other sites but cannot find a solution to my problem with my ggtitle(). I know there are other examples out there, but I haven't been able to apply them directly to my issue successfully.

我正在尝试做一个多第一行是粗体,大小= 10,然后在第一行下面是第二行(可能是第三行),非粗体,大小= 8的更具描述性的行。踢球者是我试图在y轴上进行左对齐。 这个左边的理由是什么使得这个问题是唯一的,因为以前的答案,包括由主持人链接的答案,使用 atop(),它不允许左对齐或不

What I am trying to do is make a multi-line plot title where the first line is bold, size = 10 and then underneath that first line is a second (and potentially third), more descriptive line(s) in non-bold, size=8. The kicker is that I'm trying to make this left justified over the y-axis. This left justification is what makes this question unique because previous answers, including the one linked by a moderator, use atop() which does not allow left justification or does not include it in the answer linked.

这是我的情节目前的样子:

Here is what my plot currently looks like:

title <- paste("An analysis of the mtcars dataset  ")
subheader <- paste("How does mpg change by   \n number of cyl?")

ggplot(mtcars, aes(mpg,cyl))+ 
  geom_smooth(aes(fill="Mean",level=0.095)) +
  ggtitle(paste0(title,"\n",subheader)) +
  scale_fill_grey(name='95% Confidence\n Interval',start=.65,end=.65) +
  theme(plot.title = element_text(size = rel(2.0),hjust=-.1,face="bold"))

我哈我试过用 bquote() mtext() atop(),甚至是一个又脏又臭的 paste(),其中包含额外的空格来推送标题....但我一直无法找到解决方案。

I have tried using bquote(), mtext(), atop(), and even a grungy paste() with extra spaces included to push the title....but I haven't been able to find a solution.

请让我知道您是否有任何问题或需要澄清。我感谢任何和所有的帮助!

Please, let me know if you have any questions or need any clarification. I appreciate any and all help!

推荐答案

这是一种使用自定义注释的方法。理由很简单,但您必须手动确定文本位置的坐标。也许你可以创建一些逻辑来自动化这一步,如果你打算这么做的话。

Here's a way to use custom annotation. The justification is straightforward, but you have to determine the coordinates for the text placement by hand. Perhaps you could create some logic to automate this step if you're going to do this a lot.

library(grid) 

title <- paste("An analysis of the mtcars dataset  ")
subheader <- paste("How does mpg change by\nnumber of cyl?")

p1 = ggplot(mtcars, aes(mpg,cyl))+ 
  geom_smooth(aes(fill="Mean",level=0.095)) +
  scale_fill_grey(name='95% Confidence\n Interval',start=.65,end=.65) 

p1 = p1 + 
  annotation_custom(grob=textGrob(title, just="left", 
                                  gp=gpar(fontsize=10, fontface="bold")),
                    xmin=9.8, xmax=9.8, ymin=11.7) +
  annotation_custom(grob=textGrob(subheader, just="left", 
                                  gp=gpar(fontsize=8, lineheight=1)),
                    xmin=9.8, xmax=9.8, ymin=10.4) +
  theme(plot.margin=unit(c(4,rep(0.5,3)), "lines"))

# Turn off clipping
p1 <- ggplot_gtable(ggplot_build(p1))
p1$layout$clip[p1$layout$name == "panel"] <- "off"
grid.draw(p1) 

< img src =https://i.stack.imgur.com/M5vkU.pngalt =在这里输入图片描述>

这篇关于多行ggplot标题具有不同的字体大小,脸部等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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