R 具有不同字体大小、字体等的多行打印标题
我搜索了SO和其他网站,但找不到解决我的R 具有不同字体大小、字体等的多行打印标题,r,ggplot2,R,Ggplot2,我搜索了SO和其他网站,但找不到解决我的ggtitle()问题的方法。我知道还有其他的例子,但我还不能将它们直接应用到我的问题上 我想做的是制作一个多行打印标题,其中第一行粗体,大小=10,然后在第一行下面是第二行(可能是第三行),更具描述性的非粗体行,大小=8。更重要的是,我正试图使这个左对齐y轴这个左对齐是这个问题唯一的原因,因为以前的答案,包括由主持人链接的答案,使用了top(),它不允许左对齐或不包含在链接的答案中。 以下是我的绘图当前的样子: title您可以使用这种方法,但对其进行
ggtitle()
问题的方法。我知道还有其他的例子,但我还不能将它们直接应用到我的问题上
我想做的是制作一个多行打印标题,其中第一行粗体,大小=10,然后在第一行下面是第二行(可能是第三行),更具描述性的非粗体行,大小=8。更重要的是,我正试图使这个左对齐y轴这个左对齐是这个问题唯一的原因,因为以前的答案,包括由主持人链接的答案,使用了top()
,它不允许左对齐或不包含在链接的答案中。
以下是我的绘图当前的样子:
title您可以使用这种方法,但对其进行正当化是非常挑剔的。由于top
设计用于格式化方程式,因此它会自动居中,因此您需要在较长的标题前插入空格并使用hjust
来绕过这一点。这种方法似乎对标题大小也有一个上限;必须有空间让空间向左移动
我管理的是:
library(ggplot2)
title <- paste(" An analysis of the mtcars dataset")
subheader <- paste("How does mpg change by number of cyl?")
ggplot(mtcars, aes(mpg,cyl)) +
geom_smooth(aes(fill="Mean",level=0.095)) +
ggtitle(bquote(atop(bold(.(title)), atop(.(subheader), "")))) +
scale_fill_grey(name='95% Confidence\n Interval',start=.65,end=.65) +
theme(plot.title = element_text(size = rel(1.3),hjust=-.6,face="bold"))
库(ggplot2)
title这里有一种使用自定义注释的方法。对齐很简单,但必须手动确定文本放置的坐标。如果您经常这样做,也许您可以创建一些逻辑来自动化这一步骤
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)
库(网格)
title以下是使用tableGrob将文本拆分为多行的三种方法
title我想当你删除这个主题(plot.title=element\u text(size=rel(2.0),hjust=-.1,face=“bold”))时,你会得到你想要的吗?是吗?@MLavoie当我删除最后一行时,我得到,如果我不够清楚,我道歉,但这不是我想要的。我希望顶行加粗,字体更大,所有行在y轴上方左对齐。@ScottJacobs我在以前的尝试中遇到过这一点和许多其他问题,但如果使用这种方法,似乎不能同时使用顶行()
和左对齐使用hjust()
或vjust()
。
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)
title <- paste("An analysis of the mtcars dataset ")
subheader <- paste("How does mpg change by \nnumber of cyl?")
library(gridExtra)
library(grid)
table_label <- function(label, params=list()) {
params1 <- modifyList(list(hjust=0, x=0, fontsize=12, fontface=2), params)
params2 <- modifyList(list(hjust=0, x=0, fontsize=8, fontface=1), params)
mytheme <- ttheme_minimal(core = list(fg_params = params2),
colhead = list(fg_params = params1))
disect <- strsplit(label, "\\n")[[1]]
m <- as.matrix(disect[-1])
g <- tableGrob(m, cols=disect[1], theme=mytheme)
g$widths <- unit(1,"npc")
g
}
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_line()
## add a title, note: centering is defined wrt the whole plot
grid.arrange(p, top=table_label(paste0(title,"\n",subheader),
params=list(x=0.1)))
## to align precisely with the panel, use gtable instead
library(gtable)
titleify <- function(p, label, ...){
g <- ggplotGrob(p)
title <- table_label(label, ...)
g <- gtable_add_grob(g, title, t=1, l=4)
g$heights <- grid:::unit.list(g$heights)
g$heights[1] <-list(sum(title$heights))
grid.newpage()
grid.draw(g)
}
titleify(p, paste0(title,"\n",subheader))
## we can also hack ggplot2 to define a custom element for the title
## though be warned the hardware supporting your computer may be damaged by head banging
element_custom <- function() {
structure(list(), class = c("element_custom", "element_text"))
}
element_grob.element_custom <- function(element, label="", ...) {
table_label(label)
}
# default method is unreliable
heightDetails.gtable <- function(x) sum(x$heights)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_line() +
ggtitle(paste0(title,"\n",subheader))+
(theme_grey() %+replace% theme(plot.title = element_custom()))