控制并排基础图形和ggplot2图形的列宽 [英] controlling column widths for side by side base graphic and ggplot2 graphic

查看:244
本文介绍了控制并排基础图形和ggplot2图形的列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Ricardo Saporta提出了这里简单的方法可以将基本图形与ggplot图形结合成多个图形。



我用这种方法绘制左侧的基本图形和右侧的ggplot图形,其中实际包含两个图形(这是一个人造的例子,不是真实的例子,但它与我的真实例子具有相同的结构):

 库(ggplot2)
库(gridExtra)
库(plotrix)

Mvalues< - 矩阵(rpois(9,1),ncol = 3)
Mcolors < - 矩阵(rpois(9,5),ncol = 3)

par(mfrow = c(1,2))
color2D.matplot(x = Mvalues,show.values = 2,cellcolors = Mcolors,
xlab =x,ylab =y,axes = FALSE,vcex = 0.4)
gg2 < - ggplot(diamonds,aes(clarity,fill = cut ))+ geom_bar(position =dodge)
ta < - do.call(arrangeGrob,list(gg2,gg2))
vp.Left< - viewport(height = unit(1, npc),width = unit(0。 5,npc),
just =left,y = 0.5,x = 0.5)
print(ta,vp = vp.Left)



非常好。但是现在我想让基本图形比ggplot图形具有更大的宽度。怎么做 ?我用 layout()函数尝试这样做失败。

解决方案

使用不同宽度的布局。注意如何将第一列定义为第二列宽度的两倍:

  layout(matrix(c(1,2, 1,3),ncol = 2,byrow = TRUE),widths = c(2,1))

设置图形

  layout(矩阵(c(1,2,1,3),ncol = 2, = x,ylab =y),widths = c(2,1))
color2D.matplot(x = Mvalues,show.values = 2,cellcolors = Mcolors,
xlab =x ,axis = FALSE,vcex = 0.4)
gg2 < - ggplot(aes(clarity,fill = cut))+ geom_bar(position =dodge)
ta < - do.call (arrangeGrob,list(gg2,gg2))

现在定义视口。你的代码几乎在那里。我简单地将它修改为右对齐,并将宽度更改为33%:

  vp < -  viewport(height =单位(1,npc),宽度=单位(0.33,npc),
只是=right,x = 1,y = 0.5)

最后,打印剩余的图形:

  print ta,vp = vp)


@Ricardo Saporta has proposed here an easy way to combine base graphics and ggplot graphics in a multiple plots figure.

I use this way to plot a base graphic at left and a ggplot graphic at right which actually contains two graphics (this is an artifical example, not a real one, but it has the same structure as my real example) :

library(ggplot2)
library(gridExtra)
library(plotrix)

Mvalues <- matrix(rpois(9,1), ncol=3)
Mcolors <- matrix(rpois(9,5), ncol=3)

par(mfrow=c(1,2))
color2D.matplot(x=Mvalues, show.values=2, cellcolors=Mcolors, 
    xlab="x", ylab="y", axes=FALSE, vcex=0.4)
gg2 <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") 
ta <- do.call(arrangeGrob, list(gg2,gg2))
vp.Left <- viewport(height=unit(1, "npc"), width=unit(0.5, "npc"), 
    just="left", y=0.5, x=0.5)
print(ta, vp=vp.Left)

Very nice. But now I want the base graphic to have a larger width than the ggplot graphics. How to do ? I have unsuccessfully tried to do so with the layout() function.

解决方案

Here you go:

Use layout with different widths. Notice how I define the first column to be double the width of column 2:

layout(matrix(c(1, 2, 1, 3), ncol=2, byrow=TRUE), widths=c(2, 1))

Set up the graphics

layout(matrix(c(1, 2, 1, 3), ncol=2, byrow=TRUE), widths=c(2, 1))
color2D.matplot(x=Mvalues, show.values=2, cellcolors=Mcolors, 
                                xlab="x", ylab="y", axes=FALSE, vcex=0.4)
gg2 <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") 
ta <- do.call(arrangeGrob, list(gg2,gg2))

Now define the viewport. Your code was almost there. I simply modified it to be right justified, and changed the width to be 33%:

vp <- viewport(height=unit(1, "npc"), width=unit(0.33, "npc"), 
               just="right", x=1, y=0.5)

Finally, print the remaining graphic:

print(ta, vp=vp)

这篇关于控制并排基础图形和ggplot2图形的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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