在ggplot2的图表标题周围放置一个灰色框 [英] Place a grey box around chart title in ggplot2

查看:487
本文介绍了在ggplot2的图表标题周围放置一个灰色框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在ggplot中使用 facet 选项时,我们会在标题(3,4,5)附近获得一个漂亮的灰色框。

  library(ggplot2)

data(mtcars)

ggplot(mtcars,aes(cyl))+ geom_bar()+ facet_wrap(〜gear)+ theme_bw()

我们如何放置一个类似的灰色框当我们不使用 facet 选项时,在图表标题周围?

  ggplot(mtcars,aes(cyl))+ geom_bar()+ theme_bw()+ 
ggtitle(你怎么把灰盒子放在我身边??)


解决方案

既然你在问怎么做没有方面,这严格来说不是一个答案,而仅仅是为了它出来,一个快速和肮脏的方式将是作弊,并使用一个facet毕竟。例如,

  mtcars $ title<  - 你怎么把灰盒子放在我身边? 
ggplot(mtcars,aes(cyl))+ geom_bar()+ theme_bw()+
facet_grid(。〜title)

可以做到这一点。


when we use the facet option in ggplot, we get a beautiful grey box around the headings (3,4,5).

library(ggplot2)

data(mtcars)

ggplot(mtcars, aes(cyl)) + geom_bar() + facet_wrap(~gear) + theme_bw()

How can we place a similiar grey box around the chart title when we do no use the facet option?

ggplot(mtcars, aes(cyl)) + geom_bar() + theme_bw() +
  ggtitle("How do you put a grey box around me??")

解决方案

Since you are asking how to do it without facets, this is strictly speaking not an answer, but just to point it out, one quick and dirty way would be to "cheat" and to use a facet after all. For instance,

mtcars$title <- "How do you put a grey box around me??"
ggplot(mtcars, aes(cyl)) + geom_bar() + theme_bw() +
  facet_grid(. ~ title)

does the trick.

这篇关于在ggplot2的图表标题周围放置一个灰色框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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