I have a dataframe filter
, which is a subset of dataframe df2
, which was made with dyplr's mutate()
function.
I want to loop through some columns and make scatterplots with them.
My loop:
colNames <- names(filter)[9:47]
for(i in colNames){
ggplot(filter, aes(x=i, y=CrimesPer10k)) +
geom_point(color="#B20000", size=4, alpha=0.5) +
geom_hline(yintercept=0, size=0.06, color="black") +
geom_smooth(method=lm, alpha=0.25, color="black", fill="black")
}
Yet I get no output, nor any errors.
What am I doing wrong?
You need to explicitly
print()
the object returned byggplot()
in afor
loop because auto-print()
ing is turned off there (and a few other places).You also need to use
aes_string()
in place ofaes()
because you aren't usingi
as the actual variable infilter
but as a character string containing the variable (in turn) infilter
to be plotted.Here is an example implementing both of these: