简体   繁体   中英

Getting a glm line in geom_smooth in R

I want to create a scatterplot with a dichotomous variable on the y-axis and a continuous variable on the x-axis. In addition to that, I want to display a S-shaped line showing the relationship between those two variables. After watching a tutorial , I transform the predictive variable to a factor with two values and used that line of code: geom_smooth(method = "glm", se = FALSE, method.args = list(family = "binomial")) . But I always get a warning message.

structure(list(country = structure(c(8L, 9L, 4L, 5L, 10L, 1L, 
7L, 6L, 3L, 12L, 2L, 11L), .Label = c("Lebanon", "Tunesia", "Palestine", 
"Iraq", "Jordan", "Morocco", "Libya", "Algeria", "Egypt", "Kuwait", 
"Yemen", "Sudan", ""), class = "factor"), women_divorce = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Agree/strongly agree", 
"Disagree/strongly disagree", "Don't know"), class = "factor"), 
    n = c(1608L, 1576L, 1975L, 1881L, 892L, 2098L, 1416L, 1714L, 
    2006L, 828L, 1900L, 1276L), prop = c(0.703104503716659, 0.676975945017182, 
    0.810755336617406, 0.790004199916002, 0.653001464128843, 
    0.874895746455379, 0.743697478991597, 0.751754385964912, 
    0.811488673139159, 0.481675392670157, 0.816852966466036, 
    0.540220152413209), reg_var = structure(c(2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor"), 
    life_exp = c(76.693, 71.825, 70.454, 74.405, 75.398, 78.875, 
    72.724, 76.453, 73.895, 65.095, 76.505, 66.096)), row.names = c(NA, 
-12L), groups = structure(list(country = structure(1:12, .Label = c("Lebanon", 
"Tunesia", "Palestine", "Iraq", "Jordan", "Morocco", "Libya", 
"Algeria", "Egypt", "Kuwait", "Yemen", "Sudan", ""), class = "factor"), 
    .rows = structure(list(6L, 11L, 9L, 3L, 4L, 8L, 7L, 1L, 2L, 
        5L, 12L, 10L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

I used this code:

life_exp_var$reg_var <- as.factor(life_exp_var$reg_var)

ggplot(life_exp_var, aes(life_exp, reg_var)) +
  geom_point() +
  geom_smooth(method = "glm", se = FALSE, method.args = list(family = "binomial")) +
  geom_text(aes(label = country, vjust = 1), size = 5) +
  scale_x_continuous(limits = c(65, 80), breaks = seq(65, 80, by = 2.5)) +
  labs(title = "Relationship between a country's average life expectancy and approval of equal divorce rights for women",
       x = "Life expactancy (in years)", y = "Approval of equal divorce rights (%)") +
  theme_minimal() +
  theme(plot.title = element_text(size = 20, hjust = 0),
        axis.text = element_text(size = 20, colour = "black"),
        axis.title.x = element_text(size = 20, colour = "black"),
        axis.title.y = element_text(size = 20, colour = "black"),
        axis.ticks.x = element_line(),
        axis.ticks.y = element_line(),
        panel.border = element_rect(colour = "black", fill = NA),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_line(color = "grey"))

Getting this plot:

在此处输入图像描述

As you can see, there is no glm line created. I get this warning message:

Warning message:
Computation failed in `stat_smooth()`:
y values must be 0 <= y <= 1

What am I doing wrong?

Greetings

What you has to do is to transform the y variable to numeric, so, instead of the line

life_exp_var$reg_var <- as.factor(life_exp_var$reg_var)

you should use

life_exp_var$reg_var <- as.numeric(as.character(life_exp_var$reg_var))

You can take a look here: https://ggplot2.tidyverse.org/reference/geom_smooth.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM