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)]?
假设我有两个数据集,一个具有y范围[min0:max0],另一个具有y范围[min1:max1]。如何将两个箱形图放在一个图中,并且具有合理的y范围[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.
但这只会保留第一个图的y范围并且也会挤压两个图,而我希望它们是方形的。
Any ideas?
有任何想法吗?
1 个解决方案
#1
35
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)
You may also want to think about a way to do this using grid graphics, either by the lattice
or ggplot2
packages.
您可能还想通过grid或ggplot2包来考虑使用网格图形来实现此目的的方法。
Here's one attempt in ggplot2:
这是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)
And in lattice:
在格子中:
# Using lattice
library(lattice)
bwplot(~ val|sub.type+d.type ,d)
Note how the grid-based solutions keep you from ever having to specify limits; you specify structure and the software does the rest.
请注意基于网格的解决方案如何使您不必指定限制;你指定结构,软件完成其余的工作。
#1
35
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)
You may also want to think about a way to do this using grid graphics, either by the lattice
or ggplot2
packages.
您可能还想通过grid或ggplot2包来考虑使用网格图形来实现此目的的方法。
Here's one attempt in ggplot2:
这是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)
And in lattice:
在格子中:
# Using lattice
library(lattice)
bwplot(~ val|sub.type+d.type ,d)
Note how the grid-based solutions keep you from ever having to specify limits; you specify structure and the software does the rest.
请注意基于网格的解决方案如何使您不必指定限制;你指定结构,软件完成其余的工作。