Al Vinjamur
Al Vinjamur

Reputation: 1

ggplot2 - passing dataframe with column names

I looked at other solutions but cannot get a logical within ggplot to work correctly. I have the following function. A dataframe is passed alongwith two columns to plot as a scatter plot.

scatter_plot2 <- function(df, xaxis, yaxis){
       b <- ggplot(data = df, aes_string(xaxis, yaxis), environment = environment())
       gtype <- geom_point(aes(alpha = 0.2, color = yaxis > 0)) 
       sm <- geom_smooth(formula = xaxis ~ yaxis, color="black")
       b + gtype + sm + theme_bw() 
    }

which I call using :

scatter_plot2(train_df, "train_df$signal", "train_df$yhat5")

===

The color = yaxis > 0

is intended to plot points above (yaxis) 0 in "green" and ones below in "red". While i'm able to get the string names to correctly display on the axis, I'm not able to get the logical to work correctly.

Please help.

Upvotes: 0

Views: 96

Answers (1)

Jake Kaupp
Jake Kaupp

Reputation: 8072

Since you're creating your own function for this, just calculate the needed color ahead of time. Since you're passing in a data frame and the variables, you'll need to use some standard evaluation (you're already doing this using aes_string).

I cleaned up the code a bit, putting the ggplot statement into a single chain,, making some aes calls explicit, and making your smooth formula y~x. You also don't want to use $ when passing in the variables, just pass quoted names.

library(dplyr)
library(ggplot2)

scatter_plot2 <- function(df, xaxis, yaxis){

  df <- mutate_(df, color = ~ifelse(yaxis > 0, "green", "red"))

  ggplot(data = df, aes_string(x = xaxis, y = yaxis)) +
      geom_point(aes(alpha = 0.2, color = color)) +
      geom_smooth(formula = y ~ x, color =" black") +
      scale_color_identity() +
      theme_bw() 
}

The call would be (using iris for an example):

scatter_plot2(iris, "Sepal.Width", "Sepal.Length")

resulting in:

enter image description here

Upvotes: 1

Related Questions