I would expected that
data <- data.frame(col1 = c(1, 2, 2, 3, 3, 4),
col2 = c(1, 2, 2, 1, 2, 1),
z = rnorm(6))
p1<-ggplot(data, aes(x=col1))
for(idx in unique(data$col2)){
p1<-p1 + geom_bar(subset = .(col2 == idx), aes(y = ..count..),fill = "blue", alpha = 0.2)
}
p1
have the same output like
p1<-ggplot(data, aes(x=col1))
p1<-p1 + geom_bar(subset = .(col2 == 1), aes(y = ..count..),fill = "blue", alpha = 0.2)
p1<-p1 + geom_bar(subset = .(col2 == 2), aes(y = ..count..),fill = "blue", alpha = 0.2)
p1
but it istn't. So how could I produce in the for loop the same output like in the second example.
idx == 2at the end and that's what both layers will see.