geom_smooth does not show up when text option is added to aes

Hi everybody,

I want to plot the data and added a smooth curve (LS line for example) but the smooth curve does not show up when the text option is added to aes() (Eventually I want to use ggplotly and need to add additional information to the tooltip, hence the text option). Could somebody help me sort out the problem? I am using Windows 10, R 4.0, R-Studio 1.3.957, ggplot2 3.3.0. Thanks.


library(ggplot2)
library(plotly)
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(tibble)

df = tibble(Sample = letters[1:5], GMT=c(8,14,18,21,22), RSD=c(47,6,29,36,30))

tmpplot1=ggplot(df, aes(x=GMT, y=RSD))+
  geom_smooth(method='lm') +
  geom_point()

tmpplot2=ggplot(df, aes(x=GMT, y=RSD, text=paste('Sample:', Sample)))+
  geom_smooth(method='lm') +
  geom_point()

tmpplot1
#> `geom_smooth()` using formula 'y ~ x'

tmpplot2
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-06-04 by the reprex package (v0.3.0)

It seems to work if you include the aes(text = ...) just in geom_point() and not for all the geoms.

library(tidyverse)

df = tibble(Sample = letters[1:5], GMT=c(8,14,18,21,22), RSD=c(47,6,29,36,30))

p <- ggplot(df, aes(x=GMT, y=RSD)) +
  geom_smooth(method='lm') +
  geom_point(aes(text=paste('Sample:', Sample)))
#> Warning: Ignoring unknown aesthetics: text

p
#> `geom_smooth()` using formula 'y ~ x'

plotly::ggplotly(p)

Created on 2020-06-04 by the reprex package (v0.3.0)

@mfherman. Yes, it works; Thank you very much.

Curious if you wouldn't mind marking this response as a solution? FAQ: How do I mark a solution?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.