由'ggpair'和'ggplot'并排生成的图 [英] plots generated by 'ggpair' and 'ggplot' side-by-side
本文介绍了由'ggpair'和'ggplot'并排生成的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定两个ggplots,我们可以并排排列它们。
library(ggplot2)
library( gridExtra)
#数据
数据(虹膜)
df< - data.frame(y = rnorm(100))
#绘制$ (x = 1:100,y = y))+ geom_line()(其中,b =
grid.arrange(p1,p2,ncol = 2)
如果其中一个图是 ggpair
对象,我们该怎么做?
library(GGally)
p1 < - ggpairs(iris,colors ='Species')
p2 < ; ggplot(df,aes(x = 1:100,y = y))+ geom_line()
grid.arrange(l,p2,ncol = 6)
# list(data = list(Sepal.Length = c(5.1,4.9,4.7,
#4.6,:只在'gList'中允许'grobs'
解决方
有关您可以创建一个从ggpairs情节网格对象的快捷方式。从?grid.grab
帮助页面,它可能比Roland编写新的ggpairs打印方法的方法稍微不够健壮。 * ...不保证忠实地复制所有可能的网格输出。(尽管使用 wrap
参数表示它应该,但它超出了我的肯定)
library(ggplot2)
library(grid)
library(gridExtra)
library(GGally)
df < - data.frame(y = rnorm(100))
p1 < - ggpairs(iris,colors ='Species')
p2 < - ggplot(df,aes (x = 1:100,y = y))+ geom_line()
g < - grid.grabExpr(print(p1))
grid.arrange(g,p2,widths = c(0.8,0.2))
p>
Given two ggplots, we can arange them side-by-side
library(ggplot2)
library(gridExtra)
# Data
data(iris)
df <- data.frame(y = rnorm(100))
# Plotting
p1 <- qplot(data=iris, Sepal.Width, Sepal.Length)
p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line()
grid.arrange(p1, p2, ncol=2)
How can we do it if one of the plots is a ggpair
object?
library(GGally)
p1 <- ggpairs(iris, colours='Species')
p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line()
grid.arrange(l, p2, ncol=6)
# Error in gList(list(list(data = list(Sepal.Length = c(5.1, 4.9, 4.7,
# 4.6, : only 'grobs' allowed in "gList"
解决方案
For a quick way you can create a grid object from the ggpairs plot. It is perhaps a bit less robust than Roland's method of writing a new ggpairs print method as from the ?grid.grab
help page. * ... is not guaranteed to faithfully replicate all possible grid output." (although using wrap
argument indicates it should, but its beyond my ken)
library(ggplot2)
library(grid)
library(gridExtra)
library(GGally)
df <- data.frame(y = rnorm(100))
p1 <- ggpairs(iris, colours='Species')
p2 <- ggplot(df, aes(x=1:100, y=y)) + geom_line()
g <- grid.grabExpr(print(p1))
grid.arrange(g, p2, widths=c(0.8,0.2))
这篇关于由'ggpair'和'ggplot'并排生成的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文