I have this dataframe:
set.seed(50)
data <- data.frame(age=c(rep("juv", 10), rep("ad", 10)),
sex=c(rep("m", 10), rep("f", 10)),
size=c(rep("large", 10), rep("small", 10)),
length=rnorm(20),
width=rnorm(20),
height=rnorm(20))
Using this dataframe, I need to make 9 scatterplots, that plot each continuous variable against each factor variable. So I need to make these scatterplots:
library(ggplot2)
ggplot(data, aes(length, width, group=age, colour=age)) + geom_point()
ggplot(data, aes(length, height, group=age, colour=age)) + geom_point()
ggplot(data, aes(width, height, group=age, colour=age)) + geom_point()
ggplot(data, aes(length, width, group=sex, colour=sex)) + geom_point()
ggplot(data, aes(length, height, group=sex, colour=sex)) + geom_point()
ggplot(data, aes(width, height, group=sex, colour=sex)) + geom_point()
ggplot(data, aes(length, width, group=size, colour=size)) + geom_point()
ggplot(data, aes(length, height, group=size, colour=size)) + geom_point()
ggplot(data, aes(width, height, group=size, colour=size)) + geom_point()
However, I want to be able to make these 9 scatterplots using a function. Something like this:
makeScatterplots <- function(x.variable, y.variable, my.factor){
ggplot(dataframe, aes(x.variable, y.variable, group=my.factor, colour=my.factor)) + geom_point()
}
How can I make an function, which takes the x variables, y variables and grouping factors, that will compute these 9 scatterplots?