2016-02-11 117 views
1

我對ggplot創建下圖,需要標註在每個圖作爲字幕的一些信息,該圖是這樣的: enter image description here 對於標題和副標題的目的,我寫了下面的代碼:ggplot字幕和標題位置 - 重疊冠軍

ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), "")))) 

然而,因爲它可以看到標題當前重疊,我無法找到一種方法將它們重新定位,而不重疊:

plot.title <- "Link A" 
Common <- paste("Percentage:", "10%", sep=" ") 
Average <- paste("Average:", "83", sep= " ") 
plot.subtitle <- paste(Common, AverageSearchSpace, sep="\n") 

和ggplot作爲添加此平。

我想知道分離重疊標題的解決方案是什麼。我試圖增加主題()中的情節邊際:

theme(plot.margin = unit(c(2, 2, 2, 2), "cm") 

但是,這並沒有幫助。

而且,我試過如下:

plot.title = element_text(size = 85,colour="black", vjust = -2) 

這似乎分別調整所有標題的位置,而不是字幕和標題。

另外,我找不到任何命令在theme()中,比如plot.subtitle來排列它的位置。它似乎並不存在。

任何幫助代碼片或相關鏈接表示讚賞。謝謝。

+1

你沒有提供一個可重現的例子,所以很難提供幫助;這可能會有所幫助,最近也被問到http://stackoverflow.com/questions/35367095/multi-line-ggplot-title-with-different-font-size-face-etc?noredirect=1#comment58440550_35367095 – MLavoie

+0

非常感謝。我搜索它,但從來沒有想過在搜索上寫多行。我正在尋找多行字幕標題等無法找到這個。謝謝你的時間。 @MLavoie – kukushkin

回答

2

標題和副標題的位置自動調節,但是,如果標題/字幕有一個以上的線與這種定位顯然失敗,如果大小比較大,就像你的情況一樣。所以因此重疊。 解決這個問題的最簡單方法是簡單地爲您的標題添加一個額外的(空白)行。因爲標題隨後向上移動,所以您需要調整邊距。

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. 

導致圖像:enter image description here

還有另一種選擇,如果你想要更多的控制:我們只爲標題或副標題的情節上面ggtitle和使用annotate爲其他。 (請考慮下週的作業;-))

+0

非常感謝您的回答。我在開始時使用了註釋。不過,我顯然也沒有做到這一點。我會再次嘗試使用註釋選項來更好地學習。精彩的回答,再次感謝! @RHA – kukushkin

0

你可以使用這一招

enter image description here

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())) 
+0

感謝您的評論,並指引我找到我無法找到的其他鏈接。感謝。 – kukushkin