R 如何添加不同大小和颜色的ggplot2字幕?

R 如何添加不同大小和颜色的ggplot2字幕?,r,ggplot2,subtitle,R,Ggplot2,Subtitle,我正在使用ggplot2改进降水条形图 以下是我想要实现的一个可复制的示例: library(ggplot2) library(gridExtra) secu <- seq(1, 16, by=2) melt.d <- data.frame(y=secu, x=LETTERS[1:8]) m <- ggplot(melt.d, aes(x=x, y=y)) + geom_bar(fill="darkblue") + labs(x="Weather station

我正在使用ggplot2改进降水条形图

以下是我想要实现的一个可复制的示例:

library(ggplot2)
library(gridExtra)
secu <- seq(1, 16, by=2)
melt.d <- data.frame(y=secu, x=LETTERS[1:8])
m <- ggplot(melt.d, aes(x=x, y=y)) +
  geom_bar(fill="darkblue") + 
  labs(x="Weather    stations", y="Accumulated Rainfall [mm]") +
  opts(axis.text.x=theme_text(angle=-45, hjust=0, vjust=1),
       title=expression("Rainfall"), plot.margin = unit(c(1.5, 1, 1, 1), "cm"),
       plot.title = theme_text(size = 25, face = "bold", colour = "black", vjust = 5))
z <- arrangeGrob(m, sub = textGrob("Location", x = 0, hjust = -3.5, vjust = -33, gp = gpar(fontsize = 18, col = "gray40"))) #Or guessing x and y with just option
z
库(ggplot2)
图书馆(gridExtra)

secu您可以使用grid.arrange包装绘图,并传递自定义的基于栅格的标题

库(ggplot2)
图书馆(gridExtra)

p忽略此答案
ggplot2
2.2.0版具有标题和副标题功能。见@hrbrmstr的答案


您可以在
表达式中使用嵌套的
函数来获得不同的大小

编辑更新了ggplot2 0.9.3的代码

m <-  ggplot(melt.d, aes(x=x, y=y)) + 
     geom_bar(fill="darkblue", stat = "identity") + 
     labs(x="Weather    stations", y="Accumulated Rainfall [mm]") + 
     ggtitle(expression(atop("Rainfall", atop(italic("Location"), "")))) +
     theme(axis.text.x = element_text(angle=-45, hjust=0, vjust=1), 
     #plot.margin = unit(c(1.5, 1, 1, 1), "cm"), 
     plot.title = element_text(size = 25, face = "bold", colour = "black", vjust = -1))

m从ggplot 2 0.9.1开始,
opts
似乎已被弃用,不再起作用。这对我今天的最新版本很有用:
+ggtitle(表达式(顶部(“顶行”),顶部(斜体(“第二行”),”))
此版本使用了
gtable
函数。它允许标题中有两行文本。每行的文字、大小、颜色和字体可以独立设置。但是,该功能将仅使用单个绘图面板修改绘图

次要编辑:更新到ggplot2 v2.0.0

# The original plot
library(ggplot2)

secu <- seq(1, 16, by = 2)
melt.d <- data.frame(y = secu, x = LETTERS[1:8])

m <- ggplot(melt.d, aes(x = x, y = y)) + 
     geom_bar(fill="darkblue", stat = "identity") + 
     labs(x = "Weather    stations", y = "Accumulated Rainfall [mm]") + 
     theme(axis.text.x = element_text(angle = -45, hjust = 0, vjust = 1))


# The function to set text, size, colour, and face
plot.title = function(plot = NULL, text.1 = NULL, text.2 = NULL, 
   size.1 = 12,  size.2 = 12,
   col.1 = "black", col.2 = "black", 
   face.1 = "plain",  face.2 = "plain") {

library(gtable)
library(grid)

gt = ggplotGrob(plot)

text.grob1 = textGrob(text.1, y = unit(.45, "npc"), 
   gp = gpar(fontsize = size.1, col = col.1, fontface = face.1))
text.grob2 = textGrob(text.2,  y = unit(.65, "npc"), 
   gp = gpar(fontsize = size.2, col = col.2, fontface = face.2))

text = matrix(list(text.grob1, text.grob2), nrow = 2)
text = gtable_matrix(name = "title", grobs = text, 
   widths = unit(1, "null"), 
   heights = unit.c(unit(1.1, "grobheight", text.grob1) + unit(0.5, "lines"), unit(1.1,  "grobheight", text.grob2) + unit(0.5, "lines")))

gt = gtable_add_grob(gt, text, t = 2, l = 4)
gt$heights[2] = sum(text$heights)

class(gt) =  c("Title", class(gt))

gt
}

# A print method for the plot
print.Title <- function(x) {
   grid.newpage()   
   grid.draw(x)
}


# Try it out - modify the original plot
p = plot.title(m, "Rainfall", "Location", 
   size.1 = 20, size.2 = 15, 
   col.1 = "red", col.2 = "blue", 
   face.2 = "italic")

p
#原始绘图
图书馆(GG2)

secu您可能已经注意到Sandy的代码没有为“Rainsion”生成粗体标题——粗体标题的说明应该出现在top()函数中,而不是theme()函数中

ggplot(melt.d, aes(x=x, y=y)) + 
 geom_bar(fill="darkblue", stat = "identity") + 
 labs(x="Weather    stations", y="Accumulated Rainfall [mm]") + 
 ggtitle(expression(atop(bold("Rainfall"), atop(italic("Location"), "")))) +
 theme(axis.text.x = element_text(angle=-45, hjust=0, vjust=1),
 plot.title = element_text(size = 25, colour = "black", vjust = -1))

最新的ggplot2版本(即2.1.0.9000或更新版本)具有内置功能的字幕和下方的打印标题。这意味着您可以这样做:

library(ggplot2) # 2.1.0.9000+ 

secu <- seq(1, 16, by=2)
melt.d <- data.frame(y=secu, x=LETTERS[1:8])

m <-  ggplot(melt.d, aes(x=x, y=y))
m <- m + geom_bar(fill="darkblue", stat="identity")
m <- m + labs(x="Weather    stations", 
              y="Accumulated Rainfall [mm]",
              title="Rainfall",
              subtitle="Location")
m <- m + theme(axis.text.x=element_text(angle=-45, hjust=0, vjust=1)) 
m <- m + theme(plot.title=element_text(size=25, hjust=0.5, face="bold", colour="maroon", vjust=-1))
m <- m + theme(plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="black"))
m
library(ggplot2)#2.1.0.9000+

secu在GTTable中添加grobs并以这种方式制作一个奇特的标题并不难

library(ggplot2)
library(grid)
library(gridExtra)
library(magrittr)
library(gtable)

p <- ggplot() + 
  theme(plot.margin = unit(c(0.5, 1, 1, 1), "cm"))

lg <- list(textGrob("Rainfall", x=0, hjust=0, 
                    gp = gpar(fontsize=24, fontfamily="Skia", face=2, col="turquoise4")),
               textGrob("location", x=0, hjust=0, 
                        gp = gpar(fontsize=14, fontfamily="Zapfino", fontface=3, col="violetred1")),
           pointsGrob(pch=21, gp=gpar(col=NA, cex=0.5,fill="steelblue")))

margin <- unit(0.2, "line")
tg <- arrangeGrob(grobs=lg, layout_matrix=matrix(c(1,2,3,3), ncol=2),
                  widths = unit.c(grobWidth(lg[[1]]), unit(1,"null")),
                  heights = do.call(unit.c, lapply(lg[c(1,2)], grobHeight)) + margin)

grid.newpage()
ggplotGrob(p) %>%
  gtable_add_rows(sum(tg$heights), 0) %>%
  gtable_add_grob(grobs=tg, t = 1, l = 4)  %>%
  grid.draw()
库(ggplot2)
图书馆(网格)
图书馆(gridExtra)
图书馆(magrittr)
图书馆(gtable)

p vjust=-33在Linux上为我工作。我知道sub应该在情节下面,但这是我得到我想要的东西的唯一途径。出于某种原因,这使得我的情节非常小,在情节下面创造了一个巨大的空间graph@hrbrmstr现在的答案似乎是:当前版本的ggplot使用主题和元素文本,而不是分别使用选项和主题文本。此外,在当前版本的gridExtra(gridExtra_0.8.1)和ggplot2(ggplot2_0.9.3.1)中,ggplotGrob方法似乎失败。如果没有最终的
,“
,这一部分是干什么的?我不知道。我可能复制了我在其他地方看到的一个例子。可能来自上面@SandyMuspratt的回答:P-我现在明白了,
top()
类似于一个没有条的分数。因此,将第二个
top()
放在第一个的内部会得到一个小分数,文本会按比例变小。
是子分数的底部。但这似乎是不必要的-也许
top()
第二个参数有一个默认的空字符串,或者其他什么。似乎@SandyMuspratt的答案在我发布我的答案后被修改,以反映与我类似的代码。:-)嗨,这是一个惊人的解决方案。我想使用它,但不是
顶部(斜体(“位置”)
我想有一个对象:
顶部(斜体(我的字符串向量)
。我尝试了,但后来字幕计算为(我的字符串向量)。如何强制此表达式使用字符串值,而不按字面处理提供的文本?如果您在使用变量时遇到问题,您应该使用
bquote
而不是
expression
,请参见@Konrad使用对象,将
expression
替换为
bquote
并将对象包装为
()
,就像这样,对于存储在名为“main.title”的对象中的主标题和存储在名为“sub.title”的对象中的副标题:
ggtitle(bquote(top((main.title),top(italic((sub.title)),“”))
这里要归功于Didzis Elferts的回答:函数(el,elname):“plot.subtitle”中的错误不是有效的主题元素名称。请阅读答案:“最新的ggplot2版本(即2.1.0.9000或更新版本)”
library(ggplot2)
library(grid)
library(gridExtra)
library(magrittr)
library(gtable)

p <- ggplot() + 
  theme(plot.margin = unit(c(0.5, 1, 1, 1), "cm"))

lg <- list(textGrob("Rainfall", x=0, hjust=0, 
                    gp = gpar(fontsize=24, fontfamily="Skia", face=2, col="turquoise4")),
               textGrob("location", x=0, hjust=0, 
                        gp = gpar(fontsize=14, fontfamily="Zapfino", fontface=3, col="violetred1")),
           pointsGrob(pch=21, gp=gpar(col=NA, cex=0.5,fill="steelblue")))

margin <- unit(0.2, "line")
tg <- arrangeGrob(grobs=lg, layout_matrix=matrix(c(1,2,3,3), ncol=2),
                  widths = unit.c(grobWidth(lg[[1]]), unit(1,"null")),
                  heights = do.call(unit.c, lapply(lg[c(1,2)], grobHeight)) + margin)

grid.newpage()
ggplotGrob(p) %>%
  gtable_add_rows(sum(tg$heights), 0) %>%
  gtable_add_grob(grobs=tg, t = 1, l = 4)  %>%
  grid.draw()