How to add and remove geom_smooth() trendline in a ggplot2 scatter plot of a shiny app

You need something like the following

if (input$td == 'lm'){
  p1 <- p1 + geom_smooth(<your options here>) + geom_text(<your options here)
} else {
  p1 <- p1 + geom_text(<your options here)
}

That is basic ggplot2 syntax. You may want to read @hadley's book (https://github.com/hadley/ggplot2-book)

1 Like