1
ggplot2
パッケージで複数の関数をプロットしようとしています。stat_function
を使用しています。いくつかのパラメータのオプションがあるので、私はforループを使用します。私はプロットをリスト変数myplot
に保存します。問題は印刷しようとすると発生します。 print
を使用しても問題はありません。 myplot[[1]]
行はmyplot[[2]]
などと同じですが、ポイントは正しくプロットされています。同じ問題は、関数grid.arrange
を使用して1つのFigureですべてのグラフをプロットしようとするときに観察されます。forループのstat_functionとgrid.arrange
は私の例を参照してください:
library("ggplot2")
myfun <- function(x, group, a, b, e){
a * x + b + group * e
}
abe <- rbind(c(1, 2, 3), c(7, 0, -4), c(-1, -5, 8))
myplot <- list()
for (i in 1:3){
x1 <- rnorm(10, 0, 1)
x2 <- rnorm(10, 1, 1)
num <- runif(20, -10, 10)
df <- cbind(rbind(data.frame(x = x1, group = "group 1"),
data.frame(x = x1, group = "group 2")),
num)
myplot[[i]] <- ggplot(df, aes_string("x", "num")) +
geom_point(aes_string(colour = "group")) +
stat_function(aes(colour = "group 1"),
fun = function(x)
myfun(x, 0, abe[i, 1], abe[i, 2], abe[i, 3]),
geom = "line") +
stat_function(aes(colour = "group 2"),
fun = function(x)
myfun(x, 1, abe[i, 1], abe[i, 2], abe[i, 3]),
geom = "line") +
ylim(c(-10, 10)) + xlim(c(-2, 2))
}
### everything OK
for (i in 1:3){
print(myplot[[i]])
}
### points are changing but lines remain the same
myplot[[1]]; myplot[[2]]; myplot[[3]]
### again points are changing but lines remain the same
grid.arrange(myplot[[1]], myplot[[2]], myplot[[3]], ncol = 3)
私は1つのファイルにすべての数字を保存したいと私は正しくgrid.arrange
プロットラインを作るのが大好きです。
ありがとう、これは私の問題を解決しました! – Adela