简体   繁体   中英

R ggplot2 geom_smooth line not displayed if overlay text used

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. 没有带有ggplot和文本的回归线

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:

显示了ggplot回归线

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!

I can't answer exactly why you get this behavior, but you are correct and... it is odd. I did some detective work with a simple example case: df <- data.frame(x=1:1000, y=rexp(1000)) . I adjusted the global aes() terms a bunch, looking for when the line disappeared. Results are below:

g <- list(geom_point(), geom_smooth())

ggplot(df, aes(x,y)) + g  # control
ggplot(df, aes(x,y, text=x)) + g  # OK
ggplot(df, aes(x,y, text='x')) + g   # OK
ggplot(df, aes(x,y, text=paste(x))) + g  # NO LINE SHOWN

The workaround for this is already kind of in your code, which is to set inherit.aes=FALSE in geom_smooth() . For whatever reason, it doesn't like paste() being used in the text= aesthetic. This means you have to restate your x and y aesthetics in geom_smooth , but it works fine:

ggplot(df, aes(x,y, text=paste(x))) +
    geom_point() + geom_smooth(inherit.aes = FALSE, aes(x,y))

在此处输入图像描述

I tried moving "text=..." inside geom_point() , like this:

p = p + geom_point(aes(text = paste("ID ",sampleID))

Although it will give a warning message, when I generate plotly with:

ggplotly(p)

it works out.

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