2016-11-29 11 views
1

ggplot2を使用して異なるファセットにいくつかの回帰方程式をプロットしたい...と私はthis postのおかげで成功したと思う。回帰方程式を持つggplot2の複数のfacet_gridの良いgeom_text品質

しかしgeom_text同じテキストゾーンにコピーし、同じ文字を複数回として、フォントが、醜いです:

iris <- iris 
iris2 <- data.frame(iris, family = c(rep(rep(1:2,25),3))) 
iris3 <- data.frame(iris2, group = paste(iris2$Species, iris2$family, sep = "_")) 

intercept <- ddply(iris3, .(group), function(x) coefficients(lm(Petal.Length~Petal.Width,x))) 
rcarre <- ddply(iris3, .(group), function(x) summary(lm(Petal.Length~Petal.Width,x))$r.squared) 

names(rcarre) <- c("group", "R2") 
names(intercept) <- c("group", "intercept", "slope") 
coefs <- merge(intercept, rcarre) 
coefs <- data.frame(coefs, 
       eq = paste("Y=",round(coefs$intercept,2),"+",round(coefs$slope, 2),"x",", ","R2=",round(coefs$R2,2), sep = "")) 
coefs$eq = as.character(coefs$eq) 

iris4 <- merge(iris3, coefs) 

ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) + 
    geom_point() + 
    geom_smooth(method=lm, se=F) + 
    facet_grid(family~Species) + 
    geom_text(aes(label=eq, x=1.5, y=6)) + 
    theme_linedraw() 

は私が与えられたソリューションhereを使用しようとしましたが、それは私

のために動作しません。
ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) + 
    geom_point() + 
    geom_smooth(method=lm, se=F) + 
    facet_grid(family~Species) + 
    geom_text(aes(label=iris4$eq, x=1.5, y=6), data = data.frame()) + 
    theme_linedraw() 

は注釈で、私は(私が問題を理解しますが、私はそれを解決することはできません)

ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) + 
    geom_point() + 
    geom_smooth(method=lm, se=F) + 
    facet_grid(family~Species) + 
    annotate("text", x = 1.5, y = 6, label = iris4$eq) + 
    theme_linedraw() 
エラーメッセージを持っています

そして、私はcoefsテーブル(面と同じ長さ)を参照している場合、方程式はもう

ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) + 
    geom_point() + 
    geom_smooth(method=lm, se=F) + 
    facet_grid(family~Species) + 
    annotate("text", x = 1.5, y = 6, label = coefs$eq) + 
    theme_linedraw() 

誰もが解決策を持っているでしょう一致しないのですか?

ありがとうございました!

答えて

0

問題は、ggplotがデータの各行のテキストを印刷していることです。あなたは、ラベルごとに1行だけを与えることでこれを止めることができます。ここで私はdplyr::distinctでこれを行うが、

ggplot(iris4, aes(x = Petal.Width, y = Petal.Length, colour = Species)) + 
    geom_point() + 
    geom_smooth(method=lm, se=F) + 
    facet_grid(family~Species) + 
    geom_text(aes(label=eq, x=1.5, y=6), data = dplyr::distinct(iris4, eq, .keep_all = TRUE)) + 
    theme_linedraw() 
+0

_Theの問題はggplotはDATA_のすべての行のテキストを印刷しているということである他の方法があるでしょう:「私は(私は他の記事でそれを読んで)これを知っていたが、私はcouldnそれを解決する方法を見つける...あなたの答えはまさに私が必要なものです! ありがとう@RichardTelford –

関連する問題