I need to make a graph that is composed of three graphs that share the same X axis but have separate Y axes. Is this possible to do in R, or do I need to make three independent graphs and put them together in a program like Adobe Illustrator?
Asked
Active
Viewed 105 times
0
-
1It's helpful when asking a plotting question to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data we can test with. It makes it harder to help you if we have to create test data to test a possible solution. – MrFlick Dec 11 '15 at 18:22
-
2See ```ggplot2```'s documentation of ```facet_grid``` – Nancy Dec 11 '15 at 18:32
-
I agree with @MrFlick but the illustration is illustrative enough to easily deduce what OP is after. – Roman Luštrik Dec 12 '15 at 15:18
2 Answers
1
Using ggplot2
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
facet_grid(cyl ~ ., scales = "free_y") +
theme_bw()

Gregor Thomas
- 136,190
- 20
- 167
- 294
0
Using plot in the base package you can put multiple graphs in 1 window using the par command. Than just add your graphs in turning off the axis on the first 2. I would suggest reading the plot and graphics documentation to learn what you can tweak to get the plot looking good.
#############
# Data
x<-1:10
y<- 1+ x*1.2
#############
par(mfrow=c(3,1)) # allow 3 graphs in the plot window 3 rows, 1 column
# plot 1
plot(x,y, axes=F, xlab=NA, ylab=NA)
box()
axis(2,seq(min(y), max(y),1), las=2)
# plot 2
plot(x,y, axes=F, xlab=NA, ylab="Y")
box()
axis(2,seq(min(y), max(y),1), las=2)
# plot 3
plot(x,y, axes=T, xlab="X", ylab=NA)
box()
axis(2,seq(min(y), max(y),1), las=2)

Daniel Bachen
- 119
- 7