Sapply而不是ggplot2循环

时间:2018-01-23 16:15:58

标签: r ggplot2 sapply

有人可以帮我创建一个能够执行这些情节的祝福。我知道ggplot2不支持循环。

代码:

library(ggplot2)

meanX <- 5
meanY <- 5

x <- sin(1:10)
y <- 30:21
Res <-as.data.frame(cbind(x,y))

for (i in 1:nrow(Res))
{
  ggplot(Res) + geom_point(aes(x = Res$x, y = Res$y)) + 
  geom_segment(aes(x = meanX, y = meanY, xend = Res$x[i], yend = Res$y[i]), Res )
}

1 个答案:

答案 0 :(得分:2)

在这种情况下,您不需要任何循环。你可以做到

ggplot(data = Res) +
    geom_point(aes(x = x, y = y)) +
    geom_segment(aes(x = meanX, y = meanY, xend = x, yend = y))
相关问题