for循环中的多个ggplot
我使用以下代码生成了多个绘图:
set.seed(12345)
a <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean = 0, sd = 1), B=rnorm(7, mean = 0, sd = 1), C=rnorm(7, mean = 0, sd = 1))
T <- data.frame(Tlabel=LETTERS[11:20], A=rnorm(10, mean = 0, sd = 1), B=rnorm(10, mean = 0, sd = 1), C=rnorm(10, mean = 0, sd = 1))
for(i in 2:(ncol(a)-1))
{
for(j in (i+1):ncol(a))
{
r <- 0.08
win.graph(width=10, height=10, pointsize=12)
plot(a[, i],a[, j], pch=19, cex=1, panel.first=grid(col="gray", lty="dotted"))
points(T[, i],T[, j],pch=19, col="white", cex=1)
text(a[, i],a[, j], rownames(a), cex=1,col="black", pos=1)
text(T[, i],T[, j], rownames(T), cex=1,col="blue", pos=1)
arrows(x0=0, y0=0, x1=T[, i], y1=T[, j], length=0.1,col=2,lty=1)
}
}
使用 win.graph
我可以维护基础中的所有绘图。但我想把我的情节放在 ggplot2 中。以下是我的代码 ggplot2。
library(ggplot2)
for(i in 2:(ncol(a)-1))
{
for(j in (i+1):ncol(a))
{
r <- 0.08
p <- ggplot(data=a, mapping=aes(x=a[, i], y=a[, j])) + geom_point() + theme_bw()
p <- p + geom_text(data=a, mapping=aes(x=a[, i], y=a[, j], label=Glabel),
size=3, vjust=1.35, colour="black")
p <- p + geom_segment(data = T, aes(xend = T[ ,i], yend=T[ ,j]),
x=0, y=0, colour="black",
arrow=arrow(angle=25, length=unit(0.25, "cm")))
p <- p + geom_text(data=T, aes(x=T[ ,i], y=T[ ,j], label=Tlabel), size=3, vjust=0, colour="red")
print(p)
}
}
上面的代码只给了我最后一张图。我想知道如何在 ggplot2 中获得所有可能的图表。谢谢
I produced multiple plots using the following code:
set.seed(12345)
a <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean = 0, sd = 1), B=rnorm(7, mean = 0, sd = 1), C=rnorm(7, mean = 0, sd = 1))
T <- data.frame(Tlabel=LETTERS[11:20], A=rnorm(10, mean = 0, sd = 1), B=rnorm(10, mean = 0, sd = 1), C=rnorm(10, mean = 0, sd = 1))
for(i in 2:(ncol(a)-1))
{
for(j in (i+1):ncol(a))
{
r <- 0.08
win.graph(width=10, height=10, pointsize=12)
plot(a[, i],a[, j], pch=19, cex=1, panel.first=grid(col="gray", lty="dotted"))
points(T[, i],T[, j],pch=19, col="white", cex=1)
text(a[, i],a[, j], rownames(a), cex=1,col="black", pos=1)
text(T[, i],T[, j], rownames(T), cex=1,col="blue", pos=1)
arrows(x0=0, y0=0, x1=T[, i], y1=T[, j], length=0.1,col=2,lty=1)
}
}
Using win.graph
I can maintain all plots in base. But I'd like to have my plot in ggplot2. Following is my code ggplot2.
library(ggplot2)
for(i in 2:(ncol(a)-1))
{
for(j in (i+1):ncol(a))
{
r <- 0.08
p <- ggplot(data=a, mapping=aes(x=a[, i], y=a[, j])) + geom_point() + theme_bw()
p <- p + geom_text(data=a, mapping=aes(x=a[, i], y=a[, j], label=Glabel),
size=3, vjust=1.35, colour="black")
p <- p + geom_segment(data = T, aes(xend = T[ ,i], yend=T[ ,j]),
x=0, y=0, colour="black",
arrow=arrow(angle=25, length=unit(0.25, "cm")))
p <- p + geom_text(data=T, aes(x=T[ ,i], y=T[ ,j], label=Tlabel), size=3, vjust=0, colour="red")
print(p)
}
}
The above code gives me the last graph only. I wonder how can I get all possible graphs in ggplot2. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自@baptiste 的评论:
From @baptiste's comment: