This is an incremental question that refers directly to this topic:
How do I loop through column names and make a ggplot scatteplot for each one
I would like to loop through column names and make a ggplot scatteplot for each one, but I want add each time a horizintal line whose intercept depends on values in the column.
So I take that code:
Y <- rnorm(100)
df <- data.frame(A = rnorm(100), B = runif(100), C = rlnorm(100),
Y = Y)
colNames <- names(df)[1:3]
for(i in colNames){
plt <- ggplot(df, aes_string(x=i, y = Y)) +
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")
print(plt)
Sys.sleep(2)
}
I switch y with x
aes_string(x=Y, y = i))
and I want to to modify that line
geom_hline(yintercept=0, size=0.06, color="black")
...so that yintercept is not constant, but depends on i, for example:
geom_hline(yintercept=c(quantile(i, 0.25)))
So that yintercept is always the first quartile of my column.
However, it doesnt work:
Error in (1 - h) * qs[i] : non-numeric argument to binary operator
I tried different options such as aes_string, paste() etc but none of this worked.
However, it doesnt work: Error in (1 - h) * qs[i] : non-numeric argument to binary operator
I tried different options such as aes_string, paste() etc but none of this worked.