R ggplot字幕和标题位置-重叠标题

R ggplot字幕和标题位置-重叠标题,r,ggplot2,title,overlapping,R,Ggplot2,Title,Overlapping,我正在ggplot上创建以下图表,需要将每个图表上的一些信息注释为副标题,该图表如下所示: 出于标题和副标题的目的,我编写了以下代码: plot.title <- "Link A" Common <- paste("Percentage:", "10%", sep=" ") Average <- paste("Average:", "83", sep= " ") plot.subtitle <- paste(Common, AverageSearchSpace, sep

我正在ggplot上创建以下图表,需要将每个图表上的一些信息注释为副标题,该图表如下所示: 出于标题和副标题的目的,我编写了以下代码:

plot.title <- "Link A" 
Common <- paste("Percentage:", "10%", sep=" ")
Average <- paste("Average:", "83", sep= " ")
plot.subtitle <- paste(Common, AverageSearchSpace, sep="\n")
然而,正如可以看到的那样,标题目前是重叠的,我无法找到一种方法来重新定位它们而不重叠

我想知道分离重叠标题的解决方案是什么。我尝试通过以下方式增加theme()中的情节边距:

然而,这没有帮助

此外,我尝试了以下方法:

plot.title = element_text(size = 85,colour="black", vjust = -2)
这似乎调整了所有标题的位置,而不是将副标题和标题分开

此外,我在theme()中找不到任何命令,例如plot.subtitle来安排其位置。它似乎不存在


欢迎提供任何帮助代码或相关链接。谢谢。

你可以用这个把戏

require(ggplot2)
需要(额外网格)#表格b

element_grob.element_custom标题和副标题的位置是自动调整的,但是,如果标题/副标题有多行,并且大小相对较大(如您的情况),则此定位显然失败。因此,存在重叠。 解决这个问题最简单的方法就是在标题上多加一行(空白)。因为标题会向上移动,所以需要调整页边距

library(ggplot2)
library(grid)
#first some toy data, next time please provide some yourself!
data <- data.frame(x=5*rep(1:100,each=5),type=rep(c("BM1","BM2","BM3","NB1","NB2"),20),y=10*(2+rnorm(500)))

plot.title <- "Link A\n" # added an extra line here 
Common <- paste("Percentage:", "10%", sep=" ")
Average <- paste("Average:", "83", sep= " ")
plot.subtitle <- paste(Common, Average, sep="\n")
plot.tottitle <- paste(plot.title,Common, Average, sep="\n")

ggplot(data,aes(x=x,y=y,color=type))+
  geom_line() + ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), ""))))  + 
  theme(plot.title = element_text(size = 50,colour="black", vjust = 0)) +
  theme(plot.margin = unit(c(2, 0, 0, 0), "cm")) #margin adjusted because title moves off plot.
库(ggplot2)
图书馆(网格)
#首先是一些玩具数据,下次请自己提供一些!

数据你没有提供一个可复制的例子,所以很难帮助;这可能会有帮助,最近也问了很多谢谢。我搜索它,但从来没有想过写多行搜索。我正在寻找多行字幕标题等,找不到这个。谢谢你抽出时间@我非常感谢你的回答。我一开始用的是注解。然而,我显然也没能做到这一点。我将使用注释选项再试一次,以便更好地学习。回答得很好,再次感谢@谢谢你的评论,并指导我到另一个链接,我找不到。谢谢你。
plot.title = element_text(size = 85,colour="black", vjust = -2)
require(ggplot2)
require(gridExtra) # tableGrob

element_grob.element_custom <- function(element, label="", ...)  {

  mytheme <- ttheme_minimal(colhead = list(fg_params = list(parse=TRUE, fontsize = 16)),
                            core = list(fg_params = list(parse=TRUE)))
  disect <- strsplit(label, "\\n")[[1]]
  m <- as.matrix(disect[-1])
  g1 <- tableGrob(m, cols = disect[1], theme=mytheme)
  # wrapping into a gTree only because grobHeight.gtable would be too tight
  # cf. absolute.units() squashing textGrobs
  gTree(children=gList(g1), height=sum(g1$heights), 
        cl = "element_custom")
}

# gTrees don't know their size 
grobHeight.element_custom = heightDetails.element_custom = function(x, ...)
  x$height
# silly wrapper to fool ggplot2's inheritance check...
element_custom <- function() {
  structure(list(), class = c("element_custom", "element_text"))
}

title <- c("First~line \n italic('wait, a second')\n integral(f(x)*dx, a, b)")


ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() + ggtitle(title) +
  (theme_grey() %+replace% theme(plot.title  = element_custom()))
library(ggplot2)
library(grid)
#first some toy data, next time please provide some yourself!
data <- data.frame(x=5*rep(1:100,each=5),type=rep(c("BM1","BM2","BM3","NB1","NB2"),20),y=10*(2+rnorm(500)))

plot.title <- "Link A\n" # added an extra line here 
Common <- paste("Percentage:", "10%", sep=" ")
Average <- paste("Average:", "83", sep= " ")
plot.subtitle <- paste(Common, Average, sep="\n")
plot.tottitle <- paste(plot.title,Common, Average, sep="\n")

ggplot(data,aes(x=x,y=y,color=type))+
  geom_line() + ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), ""))))  + 
  theme(plot.title = element_text(size = 50,colour="black", vjust = 0)) +
  theme(plot.margin = unit(c(2, 0, 0, 0), "cm")) #margin adjusted because title moves off plot.