开发者

R: How do I put two box plots next to each other, keeping same y-range for both?

Let's say I have two data sets, one with y-range [min0:开发者_开发百科max0] and the other with y-range [min1:max1]. How can put both box plots in one plot next to each other with a sane y-range [min(min0, min1):max(max0, max1)]?

Here's what I tried:

d0 <- matrix(rnorm(15), ncol=3)
d1 <- matrix(rnorm(15), ncol=3)

par(mfrow = c(1, 2))
boxplot(d0)
usr <- par("usr")
plot.new()
par(usr = usr)
boxplot(d1, add = TRUE)

But that will only keep the first plots y-range and also squeeze both plots whereas I'd like them to be square.

Any ideas?


d0 <- matrix(rnorm(15), ncol=3)
d1 <- matrix(rnorm(15), ncol=3)

# Using base R graphics
lmts <- range(d0,d1)

par(mfrow = c(1, 2))
boxplot(d0,ylim=lmts)
boxplot(d1,ylim=lmts)

R: How do I put two box plots next to each other, keeping same y-range for both?

You may also want to think about a way to do this using grid graphics, either by the lattice or ggplot2 packages.

Here's one attempt in ggplot2:

# Using ggplot2
library(ggplot2)
d <- data.frame(d.type=c(rep(0,15),rep(1,15)),sub.type=rep(c('A','B','C'),10),val=rnorm(30))

p <- ggplot(d, aes(factor(sub.type), val)) 
p + geom_boxplot() + facet_grid(. ~ d.type)

R: How do I put two box plots next to each other, keeping same y-range for both?

And in lattice:

# Using lattice
library(lattice)
bwplot(~ val|sub.type+d.type ,d)

R: How do I put two box plots next to each other, keeping same y-range for both?

Note how the grid-based solutions keep you from ever having to specify limits; you specify structure and the software does the rest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜