2

ggplot2 を使用して、y しきい値より大きいすべての y 値に注釈を付けたいと思います。

基本パッケージをplot(lm(y~x))使用すると、自動的にポップアップする 2 番目のグラフは Residuals vs Fitted、3 番目は qqplot、4 番目は Scale-location です。これらはそれぞれ、対応する X 値を隣接する注釈としてリストすることにより、極端な Y 値に自動的にラベルを付けます。私はこのようなものを探しています。

ggplot2を使用してこのベースデフォルトの動作を実現する最良の方法は何ですか?

4

1 に答える 1

7

scale_size_area()の代わりに更新scale_area()

あなたはあなたのニーズに合うようにこれから何かを取ることができるかもしれません。

library(ggplot2)

#Some data
df <- data.frame(x = round(runif(100), 2), y = round(runif(100), 2))

m1 <- lm(y ~ x, data = df)
df.fortified = fortify(m1)

names(df.fortified)   # Names for the variables containing residuals and derived qquantities

# Select extreme values
df.fortified$extreme = ifelse(abs(df.fortified$`.stdresid`) > 1.5, 1, 0)

# Based on examples on page 173 in Wickham's ggplot2 book
plot = ggplot(data = df.fortified, aes(x = x, y = .stdresid)) +
 geom_point() +
 geom_text(data = df.fortified[df.fortified$extreme == 1, ], 
   aes(label = x, x = x, y = .stdresid), size = 3, hjust = -.3)
plot

plot1 = ggplot(data = df.fortified, aes(x = .fitted, y = .resid)) +
   geom_point() + geom_smooth(se = F)

plot2 = ggplot(data = df.fortified, aes(x = .fitted, y = .resid, size = .cooksd)) +
   geom_point() + scale_size_area("Cook's distance") + geom_smooth(se = FALSE, show_guide = FALSE)

library(gridExtra)
grid.arrange(plot1, plot2)

ここに画像の説明を入力してください

ここに画像の説明を入力してください

于 2012-04-25T09:05:23.130 に答える