2017-03-07 5 views
1

ベストフィットラインと95%予測ラインを使用して線形回帰をプロットしようとしていますが、stat_smoothまたはgeom_smoothを使用すると、画像にグラフが表示されます。線はグラフに表示されず、すべてのサイトに対してその線を作成しようとしているようです。データのモックアップは写真の下にあります。あなたの時間と助けてくれてありがとう。geom_smoothまたはstat_smoothを使用してプロットする

Site Cu Fe 
A  1 123 
B  2 123 
C  3 534 
D  4 364 
E  5 234 
F  6 634 
G  7 784 
H  8 856 

スクリーンショット: enter image description here

答えて

1

あなたが外の回帰をしようとしている観察色=サイト、あなたはどの行が返さ取得していない理由ですと。

ここでの予測の95%信頼水準間隔でベストフィットのラインです:

library(ggplot2) 

# produce data 
df1 <- structure(list(Site = c("A", "B", "C", "D", "E", "F", "G", "H"), Cu = 1:8, Fe = c(123L, 123L, 534L, 364L, 234L, 634L, 784L, 856L)), .Names = c("Site", "Cu", "Fe"), row.names = c(NA, -8L), class = "data.frame") 

# ordinary case of linear regression with 95CI band 
ggplot(data = df1, aes(x = Fe, y = Cu)) + 
    geom_point() + 
    geom_smooth(method="lm") 

enter image description here

それでも色の伝説を持っているポイントを強制する場合は、あなたが行うことができます。

# plot regression line with band and color points by Site 
ggplot(data = df1, aes(x = Fe, y = Cu)) + 
    geom_smooth(method="lm") + 
    geom_point(aes(color=Site)) 

enter image description here

あなたはサイトごとに[唯一]の観測を持っているので、私はあなたの代わりに色にgeom_pointをマッピングのポイントにラベルを付けることをお勧めしたい:

ggplot(data = df1, aes(x = Fe, y = Cu)) + 
    geom_smooth(method = "lm") + 
    geom_label(aes(label=Site)) 

enter image description here

別のオプションは、あなたがしたいことが考えられプロットサイトごとのライン、そしてあなたのモックアップデータセットは、その場合には、不完全である:

df1 <- data.frame( Site = sample(letters[1:8], 999, replace=T), 
        Fe = runif(999), 
        Cu = 1:999+rnorm(1)) 


ggplot(data = df1, aes(x = Fe, y = Cu, colour=Site)) + 
    geom_smooth(method = "lm", alpha=0.1) + 
    geom_point(alpha=0) 

enter image description here

+0

は、どうもありがとうございました私の問題を解決! –

関連する問題