首页 > 解决方案 > Adding a function curve to ggplot, not with geom_smooth()

问题描述

So I am trying to add my Bayesian logistic curve to the output I already have. For context, the coefficients of my output lead me to an equation of: y = -2.53 + .0003*x

Plot without function

Here is what I have tried, and I am getting the following error:

failed function, nothing ran for the function/line

And I do not want to use geom_smooth, as it does the following which is not the EXACT points that would be on the line. Help anybody?

geom_smooth not getting the job done

标签: rggplot2

解决方案


如果没有您的代码/数据或最小的可重现示例,很难为您的问题提供解决方案,但也许这种方法会对您有所帮助:

library(tidyverse)
#install.packages("gapminder")
library(gapminder)
gapminder %>%
  mutate(is_long = ifelse(lifeExp > 80, 1, 0)) %>%
  mutate(fit = (0.00003 * gdpPercap)^2 - .253) %>% 
  ggplot(aes(x = gdpPercap)) +
  geom_jitter(aes(y = is_long), width = 0,
              height = 0.01, alpha = 0.5) +
  geom_line(aes(y = fit), colour = "blue", lty = 2) +
  annotate("text", x = 50000, y = 0.5,
           label = paste("y = -0.253 + .00003*x", "\U00B2", sep = "")) +
  coord_cartesian(ylim = c(0,1))

示例_2.png


推荐阅读