user1021713
user1021713

Reputation: 2203

Multiple boxplots in one in R

Hi I need to plot a boxplot in R. I have two matrices a and b. I created a boxplot for a and want to create boxplot for b on the same plot for a. The boxplots of the b matrix should lie on the whiskers of the boxplot for a.

Is there a way I can do it in R ??

Upvotes: 6

Views: 25334

Answers (2)

Artem Arti
Artem Arti

Reputation: 1

Solution is simple, need to force ylim manually, because plot is trimmed by values of variable 'a':

# Our data
a = rnorm(20)
b = rnorm(20, 2, 0.3)

# The plots
boxplot(a, ylim = c(min(a,b),max(a,b)), col = 1)
boxplot(b, width = 30, col = 2, add=TRUE)

Upvotes: 0

csgillespie
csgillespie

Reputation: 60452

To add a boxplot to an existing plot, just use the argument add=TRUE, viz:

##Some data
a = rnorm(20)
b = rnorm(20, 2, 0.3)

##The plots
boxplot(a)
boxplot(b, add=TRUE, col=2)

Upvotes: 11

Related Questions