How to create multiple plots, each with same plot area size, when only one plot has axis labels?

末鹿安然 提交于 2019-12-22 04:31:19

问题


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.]


回答1:


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).




回答2:


The easiest way with base graphics is probably to make them all with no y-axis labels and then add the labels separately in an additional panel to the left of all three of them.

But tasks like this are one of the reasons why lattice (and ggplot, for that matter) were invented. You might also consider a dotplot, especially if you're a William Cleveland fan.

library(reshape2)
d2 <- data
d2$q <- rownames(d2)
d2 <- melt(d2, measure.vars=1:3, id.vars=4)
d2$q <- factor(d2$q, levels=rownames(data))

library(lattice)
barchart(q ~ value|variable, data=d2)
dotplot(q ~ value|variable, data=d2)




回答3:


I could probably piece something together using layout, but it was just faster to do it using the faceting feature of ggplot2:

data$grp <- rownames(data)
datam <- melt(data,id.vars = "grp")
ggplot(datam,aes(x = grp,y = value)) + 
    facet_wrap(~variable,nrow = 1) + 
    geom_bar(stat = "identity") + 
    coord_flip()




回答4:


Expanding on Greg Snow's answer (in light of your follow-up question) here is a minimal edit of your code that does it with base graphics:

op <- par(oma=c(0,15,0,0), mfrow=c(1,3), mar=c(5,1,4,1))
barplot(data$y1,names.arg=row.names(data), horiz=TRUE,las=1, xlim=c(0,8), main="Group 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")
par(op)

the key to avoiding the "reset" you mention is assigning all the parameters in a single call to par()



来源:https://stackoverflow.com/questions/12572341/how-to-create-multiple-plots-each-with-same-plot-area-size-when-only-one-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!