0
使用包ggplot2和iris,我想用擬合迴歸線繪製散點圖。如何在一張圖中繪製lm(log(y)〜)和lm(y〜x + x^2)的ggplot
library(ggplot2)
ggplot(data=iris, aes(x = Petal.Width, y = Petal.Length,color=Species)) +
geom_point(shape=1) +
stat_smooth(method = "lm",formula= 'Petal.Length ~ Petal.Width+I(Petal.Width^2)+SaleType+Petal.Width*Species', data=iris,
aes(x = Petal.Width, y = Petal.Length,color=Species))
**Warning message:
Computation failed in `stat_smooth()`:
variable lengths differ (found for '(weights)')**
我想到的理由得到這個警告,我有兩個獨立的變量,但現在R可以不讀物種在stat_smooth劈裂增長的顏色。我如何繪製兩條與plot(Petal.Width,fitted(fit))
相同的線。另外,如果我有另一個迴歸模型擬合相同的數據組,但是log(y),我可以將兩個迴歸模型的繪製放入同一個圖中嗎?
謝謝!我有你的想法做日誌。但是,我怎樣才能繪製二次變量的迴歸擬合線和交互。 –
我明白了。通常,在ggplot2之外運行模型(如果它們有任何複雜性)會更好。所以我會像正常一樣運行它,然後繪製模型結果。 – boshek