Let's consider the following example.
Dat is a data frame with variables y, a, b, and c.
I would like to create three scatter plots where y is on the y axis and a, b, or c is on the x axis.
I used a for loop as follows.
I know x = x in aes(x = x, y = y) is wrong. My question is how to modify x = x to make it work.
library(ggplot2)
x_vec = c("a", "b", "c")
a = 1:10
b = a+10
c = b+10
y = 1:10
Dat = data.frame(y = y, a = a, b = b, c = c)
str(Dat)
p_list = list()
for (i in 1:3) {
# i = 1
x = x_vec[i]
x
p = ggplot(Dat, aes(x = x, y = y)) +
geom_point()
p_list[[i]] = p
}
plot(p_list[[1]])
plot(p_list[[2]])
plot(p_list[[3]])
