如何在R中添加字幕并更改ggplot图的字体大小?

Dav*_*d B 27 r ggplot2

我尝试使用+opts(subtitle="text")但没有出现任何副标题.主标题确实有效(+opts(title="text")).

我还想为轴(标签和坐标)使用更大的字体,但我不知道如何做到这一点.

Bra*_*sen 46

theme_get()将显示你可以使用的"隐藏"选项opts(),发布0.91theme()

当前:

theme(axis.text.x=element_text(size=X))
theme(axis.text.y=element_text(size=X))
Run Code Online (Sandbox Code Playgroud)

前0.91:

opts(axis.text.x=theme_text(size=X))
opts(axis.text.y=theme_text(size=X))
Run Code Online (Sandbox Code Playgroud)

根据您想要的尺寸更改尺寸.

在标题中,您可以使用"\n"将剩余文本移动到新行:

当前:

labs(title="text \n more text")
Run Code Online (Sandbox Code Playgroud)

前0.91:

opts(title="text \n more text") 
Run Code Online (Sandbox Code Playgroud)

ggplot2没有"字幕"功能.但您可以在任何标签中使用\n术语来下拉线.

  • `theme_text`现在已被弃用,而是使用`element_text`.我会说更多,但我正在寻找如何自己使用`element_text`.帮助只是一个存根. (3认同)

Ben*_*ker 7

更新:ggplot 2.2.0 版可以做字幕,如本博客文章中所示

例子:

library(ggplot2)
packageVersion("ggplot2")  ## 2.2.0
d <- data.frame(x=1:5,y=1:5)
ggplot(d,aes(x,y))+
    labs(title="abc",subtitle="def")+
    ## default left-aligned: moved them to center alignment
    theme(plot.title=element_text(hjust=0.5),
          plot.subtitle=element_text(hjust=0.5))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明