Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

I know that the r plot and ggplot will help me to visualize the data but they don't allow plotting logistic regression curve when you have categorical variables as independent variables (x-axis). Is there any solution, or am I missing something? Thank you in advance.

For example:

g <- ggplot(decision_use, aes(x=decision, y=use)) + geom_point(alpha=.1) +

  geom_smooth(method = "glm", 

    method.args = list(family = "binomial"), 

    se = FALSE)


plot(decision, use)

g=glm(use~decision,family=binomial, decision_use)


With decision as types of people and use as 1 or 0.

1 Answer

0 votes
by (108k points)

In R programming you can use as.numeric() as the aesthetics of the geom_smooth() layer.

#create dummy data

decision_use <- data.frame("decision" = rep(paste("type", 1:3), 5),

                 "use" = sample(c(0, 1), replace = TRUE, size = 15))

ggplot(decision_use, aes(decision, use)) +

  geom_point(alpha=.1) +


    aes(x = as.numeric(decision)), #added aesthetic to layer

    method = "glm",

    method.args = list(family = "binomial"),

    se = FALSE


Browse Categories