如果使用覆盖文字,则不显示R ggplot2 geom_平滑线
当省略aes(文本)时,我只能在ggplot或plotly中显示回归线。显示工具提示文本或回归线,但不能同时显示两者。由于ggplot/ggplotly没有警告或错误,我真的很困惑为什么 数据帧:如果使用覆盖文字,则不显示R ggplot2 geom_平滑线,r,ggplot2,R,Ggplot2,当省略aes(文本)时,我只能在ggplot或plotly中显示回归线。显示工具提示文本或回归线,但不能同时显示两者。由于ggplot/ggplotly没有警告或错误,我真的很困惑为什么 数据帧: sum(is.na(cdata2)) [1] 0 str(cdata2) 'data.frame': 2508 obs. of 7 variables: $ POPESTIMATE2019: num 55869 223234 24686 22394 57826 ... $ ST
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
sum(is.na(CDATA 2))
[1] 0
str(CDATA 2)
“数据帧”:2508 obs。共有7个变量:
$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“阿拉巴马州奥托加”、“阿拉巴马州鲍德温”、“阿拉巴马州巴伯”、“阿拉巴马州比伯”。。。
$tcases:int 132 264 92 51 47 58 301 136 329 30。。。
$t死亡人数:国际4 8 1 1 10 3 22 0。。。
$SAHdate:日期,格式:“2020-04-04”“2020-04-04”“2020-04-04”“2020-04-04”“2020-04-04”。。。
我不能确切地回答为什么你会有这种行为,但你是正确的,而且。。。这很奇怪。我用一个简单的例子做了一些检测工作:df我回答“谢谢,但是我已经用inherit.aes=FALSE进行了10次不同的迭代”,但是我再次运行了代码,简单地使用aes(y,x)解决了这个问题。除了aes(y,x),我一定什么都有。我很感激!
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
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
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
ggplot(df, aes(x,y, text=paste(x))) +
geom_point() + geom_smooth(inherit.aes = FALSE, aes(x,y))