1
我想用ggplot2在不同的方面繪製幾個迴歸方程...並且我想我成功得益於this post。使用ggplot2在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()
任何人都會有解決的辦法?
非常感謝!
_The問題是ggplot被打印在data_中的每一行文字:我知道這(我讀了它在其他職位),但我不能」找到一種方法來解決這個問題......你的回答正是我所需要的! 非常感謝@RichardTelford –