GGally putPlot does not work in a loop

I am trying to use putplot to change the graphs on the diagonal of the ggpairs plot. I can do this one by one, but when I use a loop, the diagonal elements are the same!

library(ggplot2)
library(GGally)

p=ggpairs(iris, 
    columns=, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
    colour='Species',
    lower=list(continuous='points'), 
    axisLabels='none',  
    upper=list(continuous='blank')
)

for (i in 1:4) {
    p <- putPlot(p, ggplot(iris, aes(x=iris[,i], colour=Species)) + stat_ecdf(), i,i)
}
print(p)

Non working example

Unwinding the loop works ... why ...?

p <- putPlot(p, ggplot(iris, aes(x=iris[,1], colour=Species)) + stat_ecdf(), 1,1)
p <- putPlot(p, ggplot(iris, aes(x=iris[,2], colour=Species)) + stat_ecdf(), 2,2)
p <- putPlot(p, ggplot(iris, aes(x=iris[,3], colour=Species)) + stat_ecdf(), 3,3)
p <- putPlot(p, ggplot(iris, aes(x=iris[,4], colour=Species)) + stat_ecdf(), 4,4)

Working example

+4
source share
1 answer

This is the result of a so-called lazy estimate in ggplot. Calls to various functions ggplotdo not actually create a plot; they determine how the plot is displayed. The plot is created only, for example print(p).

, aes(...) , . , ggplot , x=iris[,i]. , i=4 x=iris[,4].

? x aes(...). :

for (i in 1:4) {
  p <- putPlot(p,ggplot(data=data.frame(x=iris[,i],Species=iris$Species), 
                        aes(x=x,colour=Species)) + stat_ecdf(), i,i)
}
print(p)

ggplot x. , refenrece to x=iris[,i] aes(...).

. .

+3

Source: https://habr.com/ru/post/1530561/


All Articles