3
使用geom_abline(...)
(以及geom_vline
和geom_hline
)是否會在「天真」使用時導致同一行的多個重疊繪圖?geom_abline(...)多次繪製數據?
例如,假設我們感興趣的是下面的方位散點圖:
library(ggplot2)
library(dplyr)
k <- 4
data.frame(id = letters[1:k],
m = rnorm(k),
b = rnorm(k))[rep(1:k, 30),] %>%
mutate(x = rnorm(n()),
eps = 0.1*rnorm(n()),
y = m*x + b + eps) %>%
ggplot(aes(x, y)) +
geom_point() +
facet_wrap(~ id) ->
p
增加一個AB-線是如下的最簡單方法:
print(p + geom_abline(aes(slope = m, intercept = b), color = 'red'))
的是,「正確的方式「來做到這一點?特別是,上面不是每個方面都繪製了30次ab線?例如,這看起來像它:
print(p + geom_abline(aes(slope = m, intercept = b + eps), color = 'red'))
如果是這樣,是否更好地做這樣的事情?
print(p + geom_abline(aes(slope = m, intercept = b),
data = Z %>% group_by(id) %>% summarize(m = unique(m), b = unique(b)),
color = 'red'))
請注意,這會產生一些視覺上無法從第一個情節中分辨的東西。我的問題是關於使用這些ggplot函數的正確方法。
'geom_abline' [使您的數據唯一](https://github.com/tidyverse/ggplot2/blob/master/R/geom-abline.r#L121)。所以沒有30x重疊繪圖。 – lukeA
@lukeA這是一個非常相關的點!事實上,我覺得這確實是我的問題。如果你把它作爲一個完整的答案,我會接受它。 – banbh
當然,我會這樣做的。 – lukeA