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

发布于 2024-09-26 03:52:09 字数 132 浏览 1 评论 0原文

我尝试使用 +opts(subtitle="text") 添加字幕,但没有显示任何内容。主标题确实有效(+opts(title="text"))。

我还想为轴(标签和坐标)使用更大的字体,但我不知道该怎么做。

I tried adding a subtitle using +opts(subtitle="text") but nothing showed up. The main title does work (+opts(title="text")).

I would also like to use a larger font for the axis (labels and coordinates), but I can't tell how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最初的梦 2024-10-03 03:52:09

theme_get() 将向您显示可以在 opts() 中使用的“隐藏”选项,0.91 后它是 theme()

当前:

theme(axis.text.x=element_text(size=X))
theme(axis.text.y=element_text(size=X))

0.91 之前:

opts(axis.text.x=theme_text(size=X))
opts(axis.text.y=theme_text(size=X))

将尺寸更改为您想要的尺寸。

对于标题,您可以使用“\n”将剩余文本移动到新行:

当前:

labs(title="text \n more text")

0.91 之前:

opts(title="text \n more text") 

ggplot2 没有“副标题”功能。但是您可以在任何标签中使用 \n 术语来下拉一行。

theme_get() will show you the "hidden" options that you can use in opts(), post 0.91 it's theme()

Current:

theme(axis.text.x=element_text(size=X))
theme(axis.text.y=element_text(size=X))

Pre 0.91:

opts(axis.text.x=theme_text(size=X))
opts(axis.text.y=theme_text(size=X))

Change size, to your desired size.

wrt the title, you can use "\n" to move the remaining text to a new line:

Current:

labs(title="text \n more text")

Pre 0.91:

opts(title="text \n more text") 

ggplot2 doesn't have "subtitle" functionality. But you can use the \n term in any of the labels to drop down a line.

王权女流氓 2024-10-03 03:52:09

更新: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))

enter图像描述在这里

Update: ggplot version 2.2.0 can do subtitles, as demonstrated e.g. in this blog post.

Example:

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))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文