How to use ggplot2 package In R Language
To plot the logistic curve using the ggplot2 package library, we use the stat_smooth() function. The argument method of function with the value “glm” plots the logistic regression curve on top of a ggplot2 plot. So, we first plot the desired scatter plot of original data points and then overlap it with a regression curve using the stat_smooth() function.
Syntax:
plot + stat_smooth( method=”glm”, se, method.args )
Parameter:
- se: determines a boolean that tells whether to display confidence interval around smooth.
- method.args: determines the method function for logistic curve.
Example: Plot logistic regression
R
# load library ggplot2 library (ggplot2) # load data from CSV df <- read.csv ( "Sample4.csv" ) # Plot Predicted data and original data points ggplot (df, aes (x=var2, y=var1)) + geom_point () + stat_smooth (method= "glm" , color= "green" , se= FALSE , method.args = list (family=binomial)) |
Output:
How to Plot a Logistic Regression Curve in R?
In this article, we will learn how to plot a Logistic Regression Curve in the R programming Language.
Logistic regression is basically a supervised classification algorithm. That helps us in creating a differentiating curve that separates two classes of variables. To Plot the Logistic Regression curve in the R Language, we use the following methods.
Dataset used: Sample4