ggplot2,更改标题大小

78
我希望我的主标题和轴标题与图表中的注释文本具有相同的字体大小。
我使用了theme_get()并发现文本大小为12,所以我在我的主题语句中也这样做了,但这没有起作用。我还尝试将相对大小设置为1,但这也没有起作用。
我希望有人能帮助我。
以下是代码:
library(ggplot2)
library(gridExtra)  #to set up plot grid
library(stringr)  #string formatting functions
library(plyr)  #rbind.fill function
library(reshape2)  #transformation of tables

dat<-data.frame(
est=c(2.75,2.95,2.86,2.99),
ucl=c(2.92,3.23,3.38,4.91),
lcl=c(2.24,1.67,2.44,1.82),
ord=c(1,2,1,2)
)
dat$varname<-c('Estimate','Predictive','Estimate','Predictive')
dat$grp<-c('Cobalt','Cobalt','Chromium','Chromium')

for (i in unique(dat$grp)) {
  dat <- rbind.fill(dat, data.frame(grp = i, ord=0,
                                    stringsAsFactors = F))
}

dat$grp_combo <- factor(paste(dat$grp, dat$ord, sep = ", "))
dat$grpN <- as.numeric(dat$grp_combo)



rng <- c(0,6)
scale.rng <-1


xstart=-(max(dat$grpN)+2)
xend=4


ThemeMain<-theme(legend.position = "none", plot.margin = unit(c(0,0,0, 0), "npc"), 
                 panel.margin = unit(c(0,0, 0, 0), "npc"), 
                 title =element_text(size=12, face='bold'),
                 axis.text.y = element_blank(), 
                 axis.text.x = element_text(color='black'),
                 axis.ticks.y = element_blank(),
                 axis.title.x = element_text(size=12,color='black',face='bold')
                 )
BlankSettings <- theme(legend.position = "none", 
                       title =element_text(size=12, face='bold'),
                       plot.margin = unit(c(0,0, 0, 0), "npc"), 
                       panel.margin = unit(c(0,0, 0, 0), "npc"),              
                       axis.text.x = element_text(color='white'),                             
                       axis.text.y = element_blank(), 
                       axis.ticks.x = element_line(color = "white"),
                       axis.ticks.y=element_blank(),
                       axis.title.x = element_text(size=12,color='white',face='bold'),
                       panel.grid = element_blank(),panel.grid.major = element_blank(),panel.background = element_blank()
                       )

pd <- position_dodge(width = 0.7)

#######################################################################################################
#MAIN PLOT
#######################################################################################################
mainPart<-
  ggplot(dat, aes(x=-grpN,y=est, ymin=lcl, ymax=ucl, group=1)) +
  scale_y_continuous(name=NULL, breaks=seq(rng[1], rng[2], scale.rng), limits=c(rng[1], rng[2]), expand=c(0,0)) + 
  ylab('Ion Concentration') +
  ggtitle('Mean with 95% HDI')+
  #geom_segment(aes(x=xstart, xend=0, y=0, yend=0), linetype=3, alpha=0.01) +
  geom_linerange(aes(linetype="1"),position=pd) +
  geom_point(aes(shape="1"), fill="white",position=pd) +
  coord_flip() +
  scale_x_continuous(limits=c(xstart,xend), expand=c(0,0))+xlab(NULL)+
  ThemeMain

#######################################################################################################
#varnameS
#######################################################################################################
# ystart & yend are arbitrary. [0, 1] is
# convinient for setting relative coordinates of
# columns
ystart = 0
yend = 1
p1 <- 
  ggplot(dat, aes(x = -varnameN, y = 0)) + 
  coord_flip() + 
  scale_y_continuous(limits = c(ystart, yend)) + 
  BlankSettings+
  scale_x_continuous(limits = c(xstart, xend), expand = c(0, 0)) + 
  xlab(NULL) + 
  ylab('') +
  ggtitle('')

studyList<-
  p1 + 
  with(unique(dat[is.na(dat$varname),c("grpN","grp")]), annotate("text",label=grp, x=-grpN,y=0, fontface='bold', hjust=0)) + #Variable Group varnames
  with(dat[!is.na(dat$var),],annotate("text",label=varname,x=-grpN,y=0.04, hjust=0))  #Variables

#######################################################################################################
#EFFECTS
#######################################################################################################
f<-function(x) round(x,2)
dat$msmt<-paste(f(dat$est),' [',f(dat$lcl),', ',f(dat$ucl),']',sep='')


effectSizes<-p1+
  annotate("text",x=-dat$grpN, y=0.25,label=ifelse(is.na(dat$varname)==T,'',dat$msmt))



grid.arrange(ggplotGrob(studyList), ggplotGrob(mainPart), 
             ggplotGrob(effectSizes), ncol = 3, widths = unit(c(0.19, 
                                                                 0.4, 0.41), "npc"))

@TrynnaDoStat比我快,所以我会将我的答案作为评论添加。我认为你需要在theme()调用中使用plot.title参数而不是仅使用title。如果您想更改字体类型,可以通过family参数进行操作(但可能无法在所有平台上运行)。 - Aleksandr Blekh
2个回答

174
+ theme(plot.title = element_text(size=22))

以下是你可以在 element_text 中修改的所有内容:

element_text(family = NULL, face = NULL, colour = NULL, size = NULL,
  hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
  color = NULL)

5
使用themeplot.title是最简单的方法。但是还有另一种选项,使用ggtext提供了Markdown(element_markdown)和HTML渲染器用于ggplot2。因此,您可以使用HTML标签来更改标题的“字体大小”等。这样做的好处是您可以轻松更改标题的各个部分,因此您可以使用HTML标签为标题的某些部分提供不同的字体大小或颜色。
以下是一个可复制的示例:
library(ggplot2)
library(ggtext)
# font size using html
ggplot(mtcars, aes(x = cyl)) +
  geom_bar() +
  ggtitle("<span style='font-size: 22pt;'>This is bigger text</font>") +
  theme(plot.title = element_markdown())

# Change parts of title font size 
ggplot(mtcars, aes(x = cyl)) +
  geom_bar() +
  ggtitle("<span style='font-size: 22pt;'>This is bigger text,</font>
          <span style='font-size: 10pt;'>This is smaller text.</font>") +
  theme(plot.title = element_markdown())

这是一个使用reprex v2.0.2创建于2022年08月24日的示例。

正如您所看到的,标题有两种不同的字体大小。欲获取更多信息和示例,请查看上面的链接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接