df <- data.frame(id = rep(1:6, each = 50), a = rnorm(50*6, mean = 10, sd = 5),
b = rnorm(50*6, mean = 20, sd = 10),
c = rnorm(50*6, mean = 30, sd = 15))
I have three variables a
,b
and c
. If I have to plot a variable for all loc.id
ggplot(df, aes(a)) + geom_histogram() + facet_wrap(~id)
I have a loop for which I have to plot a
, b
and c
.
var.names <- c("a","b","c")
for(v in seq_along(var.names)){
variable <- var.names[v]
ggplot(df, aes(x = paste0(variable))) + geom_histogram() + facet_wrap(~id)
}
This loop does not work. I was wondering how do I refer to a column in the above command by its name.My actual data has many variables and hence I was doing like this.