当只有一个绘图具有轴标签时,如何创建多个绘图,每个绘图具有相同的绘图区域大小? [英] How to create multiple plots, each with same plot area size, when only one plot has axis labels?

查看:125
本文介绍了当只有一个绘图具有轴标签时,如何创建多个绘图,每个绘图具有相同的绘图区域大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在1x3布局中绘制三个图形.只有第一个图形需要具有垂直轴标签,但是我希望所有三个绘图区域的大小都完全相同.如果没有一个或所有图形都具有轴标签,这将是没有问题的.但是,当一个带有轴标签而另外两个没有轴标签时,如何使所有三个图具有相同的大小?我正在尝试在基本图形中执行此操作,因为这是我最了解的,但是如果它们提供解决我的问题的更好方法,我将很高兴使用grid或ggplot2.

以下是一些虚假数据,我的绘图代码以及绘图本身:

# Fake Data
data = structure(list(y1 = 1:5, y2 = c(1.2, 2.4, 3.6, 4.8, 6), y3 = c(1.44, 
2.88, 4.32, 5.76, 7.2)), .Names = c("y1", "y2", "y3"), 
row.names = c("I needed 12 units for financial aid", 
              "I couldn't find any other open classes",
              "I might be adding a major or minor", 
              "The class seemed interesting", "The class fit into my schedule"
), class = "data.frame")

# Plotting code
par(mar=c(5,15,4,1))
par(mfrow=c(1,3))
barplot(data$y1,names.arg=row.names(data), horiz=TRUE,las=1, +
        xlim=c(0,8), main="Group 1")
par(mar=c(5,1,4,1))
barplot(data$y2,names.arg=row.names(data), horiz=TRUE,las=1, +
        axisnames=FALSE, xlim=c(0,8), main="Group 2")
barplot(data$y3,names.arg=row.names(data), horiz=TRUE,las=1, +
        axisnames=FALSE, xlim=c(0,8), main="Group 3")

# Reset plot options back to defaults
par(mfrow=c(1,1)
par(mar=c(5,4,4,2)+0.1)

[更新:我选择了最直接回答我的问题的答案,即基本图形,但是我建议您也查看其他解决方案,因为它们展示了如何做同样的事情在latticeggplot2中.]

解决方案

对于基本图形,您希望使用外部页边距而不是常规页边距.只需将第一个par(mar=c(5,15,4,1))替换为par(oma=c(0,15,0,0))并删除对par的第二个调用,绘图将占用相等的空间(并且轴标签将粘在左侧的外部边缘).

I want to plot three graphs in a 1x3 layout. Only the first graph needs to have vertical axis labels, but I want all three plot areas to be exactly the same size. This would be no problem if either none or all of the graphs had axis labels. But how do I get all three graphs the same size when one has axis labels and the other two don't? I'm trying to do this in base graphics because that's what I know best, but I'd be happy to use grid or ggplot2 if they provide better methods for solving my problem.

Here's some fake data, my plotting code, and the plot itself:

# Fake Data
data = structure(list(y1 = 1:5, y2 = c(1.2, 2.4, 3.6, 4.8, 6), y3 = c(1.44, 
2.88, 4.32, 5.76, 7.2)), .Names = c("y1", "y2", "y3"), 
row.names = c("I needed 12 units for financial aid", 
              "I couldn't find any other open classes",
              "I might be adding a major or minor", 
              "The class seemed interesting", "The class fit into my schedule"
), class = "data.frame")

# Plotting code
par(mar=c(5,15,4,1))
par(mfrow=c(1,3))
barplot(data$y1,names.arg=row.names(data), horiz=TRUE,las=1, +
        xlim=c(0,8), main="Group 1")
par(mar=c(5,1,4,1))
barplot(data$y2,names.arg=row.names(data), horiz=TRUE,las=1, +
        axisnames=FALSE, xlim=c(0,8), main="Group 2")
barplot(data$y3,names.arg=row.names(data), horiz=TRUE,las=1, +
        axisnames=FALSE, xlim=c(0,8), main="Group 3")

# Reset plot options back to defaults
par(mfrow=c(1,1)
par(mar=c(5,4,4,2)+0.1)

[Update: I chose the answer that most directly answered my question, which was in base graphics, but I recommend looking at the other solutions as well, as they show how to do the same thing in lattice and ggplot2.]

解决方案

For base graphics you want to use an outer margin rather than the regular margins. Just replace the first par(mar=c(5,15,4,1)) with par(oma=c(0,15,0,0)) and remove the second call to par and the plots will take equal space (and the axis labels will stick into the outer margin on the left).

这篇关于当只有一个绘图具有轴标签时,如何创建多个绘图,每个绘图具有相同的绘图区域大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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