问题描述
居中放置 le art artis的方法可以在ggplot中对齐情节标题-
library(tidyverse)
mary_poppins<-data_frame(song = c(序曲,姐姐姐妹,我领导的生活,完美的保姆,一匙糖,路面艺术家,欢乐假期,超级校准狂,保持清醒,我爱笑,一家英国银行,喂鸟,富达信托银行, Chim Chim Cher-ee,进入时间,一个人有梦想,让我们去放风筝
))
mary_poppins<-mary_poppins%&%;%
mutate(len = nchar(歌曲))
ggplot(数据= mary_poppins,aes(x =重新排序(歌曲,len),y = len))+
geom_col(fill = firebrick)+
coord_ flip()+
theme_light()+
theme(axis.title.y = element_blank(),
axis.text = element_text(size = rel(1.5)),
plot.title = element_text(size = rel(2.5),face = bold,hjust = 0.5,
margin = margin(t = 10,b = 20,unit = pt))))+
ggtitle( Mary Poppins)+
ylab(标题长度(字符))
是否可以将标题居于总绘图区的中心,即。
或者,您可以使用 gridExtra :: grid。安排
和 grid :: textGrob
创建标题,而无需目视填充。基本上,这将创建一个带有标题的单独绘图对象,并将其粘贴在顶部,与 ggplot
调用的内容无关。
首先将整个 ggplot
调用存储在变量中,例如 p1
:
grid.arrange(textGrob( Mary Poppins,
gp = gpar(fontsize = 2.5 * 11,fontface = bold)),
p1,
heights = c(0.1,1))
您必须将 theme()
设置转换为 gpar ()
。 theme_light
的基本大小为11,这是2.5 * 11的来源(以及 rel(2.5)$ c的2.5来源) $ c>)。
此处的优势就是您知道自己的标题将真正居中,而不仅仅是眼前的距离。
The "lege artis" way to center justify a plot title in ggplot - plot.title = element_text(hjust = 0.5) - centers the title over plot area excluding axis labels.
This can get ugly when the axis labels are very long, such as this plot of songs in Mary Poppins Soundtrack vs. their character length.
library(tidyverse)
mary_poppins <- data_frame(song = c("Overture", "Sister Suffragette", "The Life I Lead", "The Perfect Nanny", "A Spoonful of Sugar", "Pavement Artist", "Jolly Holiday", "Supercalifragilisticexpialidocious", "Stay Awake", "I Love to Laugh", "A British Bank", "Feed the Birds ", "Fidelity Fiduciary Bank", "Chim Chim Cher-ee", "Step in Time", "A Man Has Dreams", "Let's Go Fly a Kite"
))
mary_poppins <- mary_poppins %>%
mutate(len = nchar(song))
ggplot(data = mary_poppins, aes(x = reorder(song, len), y = len)) +
geom_col(fill = "firebrick") +
coord_flip() +
theme_light() +
theme(axis.title.y = element_blank(),
axis.text = element_text(size = rel(1.5)),
plot.title = element_text(size = rel(2.5), face = "bold", hjust = 0.5,
margin = margin(t = 10, b = 20, unit = "pt"))) +
ggtitle("Mary Poppins") +
ylab("Lenght of title (characters)")
Is there a way to center the title over the total plot area, i.e . including the area taken over by axis labels?
Alternately, you can use gridExtra::grid.arrange
and grid::textGrob
to create the title without needing to pad it visually. This basically creates a separate plot object with your title and glues it on top, independing of the contents of your ggplot
call.
First store your whole ggplot
call in a variable, e.g. p1
:
grid.arrange(textGrob("Mary Poppins",
gp = gpar(fontsize = 2.5*11, fontface = "bold")),
p1,
heights = c(0.1, 1))
You have to translate your theme()
settings to gpar()
. The base size of theme_light
is 11, which is where the 2.5*11 comes from (and the 2.5 from your rel(2.5)
).
The advantage here is that you know your title will be truly centered, not just close enough by eye.
这篇关于如何居中ggplot情节标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!