R 具有回归方程的ggplot2中多方面网格中的良好几何图形文字质量
我想用ggplot2在不同的方面绘制几个回归方程……我想我成功了,多亏了 但是geom_文本字体很难看,因为它在同一个文本区域中多次复制相同的字母:R 具有回归方程的ggplot2中多方面网格中的良好几何图形文字质量,r,plot,ggplot2,R,Plot,Ggplot2,我想用ggplot2在不同的方面绘制几个回归方程……我想我成功了,多亏了 但是geom_文本字体很难看,因为它在同一个文本区域中多次复制相同的字母: 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 &l
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()
使用annotate时,我收到一条错误消息(我理解问题,但无法解决)
如果我参考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()
有人会有解决办法吗
多谢各位 问题在于ggplot正在为数据中的每一行打印文本。您可以通过只为每个标签指定一行来停止此操作。在这里,我使用
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()
问题是ggplot正在为数据中的每一行打印文本。您可以通过只为每个标签指定一行来停止此操作。在这里,我使用
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()
问题是ggplot正在为数据中的每一行打印文本:我知道这一点(我在其他帖子上读过),但我找不到解决这个问题的方法……你的答案正是我所需要的!非常感谢@richardtelford问题是ggplot正在为数据中的每一行打印文本:我知道这一点(我在其他帖子上读过),但我找不到解决方法……你的答案正是我需要的!非常感谢@RichardTelford
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()