如何添加具有不同大小和颜色的ggplot2副标题? [英] How to add a ggplot2 subtitle with different size and colour?

查看:1391
本文介绍了如何添加具有不同大小和颜色的ggplot2副标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2来改善沉淀barlot。



以下是我想达到的一个可重现的例子:
$ b $
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 =累计降雨量[mm])+
opts(axis.text.x = theme_text(angle = -45 ,hjust = 0,vjust = 1),
title =表达式(降雨量),plot.margin =单位(c(1.5,1,1,1),cm),
图.title = theme_text(size = 25,face =bold,color =black,vjust = 5))
z < - arrangeGrob(m,sub = textGrob(Location,x = 0,hjust = -3.5,vjust = -33,gp = gpar(fontsize = 18,col =gray40)))#或者只用选项猜测x和y
z

我不知道如何避免在ggplot2上使用hjust和vjust上的猜测数字?有没有更好的方法来放置一个字幕(不只是使用\\\
,而是一个字体不同的文字颜色和大小)?

我需要能够使用与ggsave有一个pdf文件。



以下是两个相关的问题:

>

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



感谢任何帮助。

解决方案

最新的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 =气象站,
y =累计降雨量[毫米],
title =降雨量,
副标题=地点)
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,color =maroon,vjust = -1))
m < - m + theme(plot.subtitle = element_text(size = 18,hjust = 0.5,face =italic black))
m


I'm using ggplot2 to improve precipitation barplots.

Here's a reproducible example of what I want to achieve:

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

I don't know how to avoid using guessing numbers on hjust and vjust on ggplot2? Is there a better way to put a subtitle (not just using \n, but a subtitle with different text color and size)?

I need to be able to use with ggsave to have a pdf file.

Here are two related questions:

Add a footnote citation outside of plot area in R?

How can I add a subtitle and change the font size of ggplot plots in R?

Thanks for any help.

解决方案

The latest ggplot2 builds (i.e., 2.1.0.9000 or newer) have subtitles and below-plot captions as built-in functionality. That means you can do this:

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

这篇关于如何添加具有不同大小和颜色的ggplot2副标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆