R 如何在使用 cookbook-r 中的 multiplot 函数绘图时添加文本(带多行文本?)
R how to add text (with mtext?) when plotting with the multiplot function from cookbook-r
我使用 here 中的 multiplot
函数将我的大量 graph<-ggplot(...)
形式的图表与
线堆叠起来
png(filename = "qwerty.png", width = 1024, height = 1024, units = "px",
pointsize = 11, bg = "white", res = 150)
multiplot(graph1,graph2,...., layout=matrix(c(1,2,3,4,5,6,7,8), nrow=8,
byrow=TRUE))
dev.off()
我想在第一张图上方添加几行文字,例如,文字可以对某些字母进行复杂的粗体编辑。
我试着用,放在multiplot
前后
mtext(expression(paste("only ", bold("a"), " should be bold")), 1, 1)
但我收到错误
Error in mtext(expression(paste("only ", bold("a"), " should be bold")), :
plot.new has not been called yet`
用我的文字''as the graph''再叠一个空图更好吗?如果可以,我该怎么做?
这些数据可用于构建图表实例
p1 <- ggplot((subset(mtcars, gear==4)), aes(x=mpg, y=hp)) +
geom_point() +
ggtitle("4 gears")
p2<-ggplot((subset(mtcars, gear==3)), aes(x=mpg, y=hp)) +
geom_point() +
ggtitle("3 gears")
multiplot(p1, p2, cols=2)
您可以使用 grid.arrange:
而不是多图
library(gridExtra)
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = "Here your title should be inserted",nrow=1)
如果您想调整一些文本参数,以下内容可能会有所帮助:
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
保存文件:
png("example-plot.png")
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
dev.off()
我使用 here 中的 multiplot
函数将我的大量 graph<-ggplot(...)
形式的图表与
png(filename = "qwerty.png", width = 1024, height = 1024, units = "px",
pointsize = 11, bg = "white", res = 150)
multiplot(graph1,graph2,...., layout=matrix(c(1,2,3,4,5,6,7,8), nrow=8,
byrow=TRUE))
dev.off()
我想在第一张图上方添加几行文字,例如,文字可以对某些字母进行复杂的粗体编辑。
我试着用,放在multiplot
mtext(expression(paste("only ", bold("a"), " should be bold")), 1, 1)
但我收到错误
Error in mtext(expression(paste("only ", bold("a"), " should be bold")), :
plot.new has not been called yet`
用我的文字''as the graph''再叠一个空图更好吗?如果可以,我该怎么做?
这些数据可用于构建图表实例
p1 <- ggplot((subset(mtcars, gear==4)), aes(x=mpg, y=hp)) +
geom_point() +
ggtitle("4 gears")
p2<-ggplot((subset(mtcars, gear==3)), aes(x=mpg, y=hp)) +
geom_point() +
ggtitle("3 gears")
multiplot(p1, p2, cols=2)
您可以使用 grid.arrange:
而不是多图library(gridExtra)
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = "Here your title should be inserted",nrow=1)
如果您想调整一些文本参数,以下内容可能会有所帮助:
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
保存文件:
png("example-plot.png")
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
dev.off()