0

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.

Community
  • 1
  • 1
Yaahtzeck
  • 217
  • 2
  • 13

1 Answers1

0

You should call quantile(df[,i], 0.25) instead of quantile(i, 0.25) and it should work, your code would be :

for(i in colNames){
  plt <- ggplot(df, aes_string(x=Y, y = i)) +
    geom_point(color="#B20000", size=4, alpha=0.5) +
    geom_hline(yintercept=c(quantile(df[,i], 0.25)))+
    geom_smooth(method=lm, alpha=0.25, color="black", fill="black")
  print(plt)
  Sys.sleep(2)
}
Rhesous
  • 984
  • 6
  • 12
  • if you want to substract this value for 1.5 IQR (and then calculate 1.5*IQR-q25) your formula should be `c( 1.5*IQR(df[,i])-quantile(df[,i], 0.25))`. In your code you do the opposite, and you forgot a parenthesis : quantile(df[,i], 0.25 **)** - 1.5*IQR(df[,i]) – Rhesous Aug 17 '16 at 15:01