I can only get a regression line to display in ggplot or plotly when aes(text) is omitted. Either tooltip text displays or a regression line, but not both. I have been really puzzled as to why since there are no warnings or errors from ggplot/ggplotly.
Dataframe:
sum(is.na(cdata2))
[1] 0
str(cdata2)
'data.frame': 2508 obs. of 7 variables:
$ POPESTIMATE2019: num 55869 223234 24686 22394 57826 ...
$ ST : chr "AL" "AL" "AL" "AL" ...
$ pdensity : num 93 140 27 35 89 16 25 187 55 47 ...
$ STC : chr "Alabama-Autauga" "Alabama-Baldwin" "Alabama-Barbour" "Alabama-Bibb" ...
$ tcases : int 132 264 92 51 47 58 301 136 329 30 ...
$ tdeaths : int 4 8 1 1 1 1 10 3 22 0 ...
$ SAHdate : Date, format: "2020-04-04" "2020-04-04" "2020-04-04" "2020-04-04" ...
s <- ggplot(cdata2, aes(x = pdensity,
y = tcases,
size = tdeaths,
color = SAHdate,
text=paste0(
"<br>St-County: ",STC,
"<br>Pop: ",POPESTIMATE2019,
"<br>Tot Cases: ",tcases,
"<br>People/sq mile: ", pdensity,
"<br>Tot Deaths: ", tdeaths))) +
geom_point() +
geom_smooth(method = "lm", na.rm = TRUE, inherit.aes = TRUE);s
The result is no regression line.
If I comment out the text portion:
s <- ggplot(cdata2, aes(x = pdensity,
y = tcases,
size = tdeaths,
color = SAHdate)
geom_point() +
geom_smooth(method = "lm", na.rm = TRUE, inherit.aes = TRUE);s
The regression line is shown:
I tried with plotly using a ton of variations of:
fit <- lm(tcases~pdensity, data=cdata2)
ggplotly(s) %>%
add_trace(x = ~pdensity, y = ~fitted(fit),data = cdata2,mode = 'lines') %>%
add_lines(x = ~pdensity, y = ~fitted(fit),data = cdata2,mode = 'lines')
`geom_smooth()` using formula 'y ~ x'
Error in min(x, na.rm = na.rm) : invalid 'type' (list) of argument
I am stumped!