从 cmets 更新。
有几种方法可以做到这一点;正如 Axeman 建议的那样,使用 facet_ 在绘图上方创建一个条带(更改条带的格式比更改标题条更容易),或者您可以手动创建一个标题条,然后将其粘贴到绘图上。
例子
library(ggplot2)
library(gridExtra)
library(grid)
# Create dummy variable to facet on: this name will appear in the strip
mtcars$tempvar <- "Market Updates"
# Basic plot
# Manually added legend to match your expected result
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_line(aes(colour="Com")) +
scale_colour_manual(name="", values=c(Com = "#228b22") ) +
labs(x = "Date", y = "High")
使用facet_:这只会在绘图面板上添加颜色条,尽管标题居中。
p + facet_grid(. ~ tempvar) +
theme(strip.background = element_rect(fill="#228b22"),
strip.text = element_text(size=15, colour="white"))
哪个产生
使用grid 函数:这会在绘图面板上添加颜色条,但标题位于图形窗口的中心。 (您可以通过将其添加到绘图gtable 来获得更多的定位控制)
my_g <- grobTree(rectGrob(gp=gpar(fill="#228b22")),
textGrob("Market Updates", x=0.5, hjust=0.5,
gp=gpar(col="white", cex=1.5)))
grid.arrange(my_g, p, heights=c(1,9))
哪个产生