r - Set layers in ggplot2 via loop -


i 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 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 istn't. how produce in loop same output in second example.

this problem simple if subsetting yourself:

library(ggplot2) data <- data.frame(   col1 = c(1, 2, 2, 3, 3, 4),   col2 = c(1, 2, 2, 1, 2, 1),   z = rnorm(6)) ids <- unique(data$col2)  p1 <- ggplot(data, aes(col1, y = ..count..))  for(id in ids){   df <- data[data$col2 == id, ]   p1 <- p1 + geom_bar(data = df, fill = "blue", alpha = 0.2) } p1 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -