here is a problem I have with ggplot+ lapply+ subsetting problem
here a reproducible exemple
stages=as.vector(c("1","2","3","1","1","1","1","1","1","2","2","2","2","2","3","3","3","3","3","3","3","3","3","3","3","1","1"))
x13=c(0.9, 0, 0, 0.692, 0,0,0,0,0,0,0,0,0,0.53, 0.5,0,2,3,55,49,64,9,4,1,0,54,0)
y13=c(0.9, 0, 0, 0.692, 0,0,0,0,0,0,0,0,0.53,0.9, 0, 0, 0.692, 0,0,0,0,0,0,0,0,0,0.53)
countmatrix=cbind(x13,stages,y13)
countmatrix=as.data.frame(countmatrix)
p2=ggplot(data=countmatrix, aes(stages, fill = x13))+
geom_bar(position="stack", fill="gray34")+
geom_bar(data=subset(countmatrix, x13!=0), aes(fill=stages ))
p2 ````
once I put it in function to replicate over many variable it seems to not work (the subsetting part at least seems to not work.
here is the code I used.
barplotseries <- function(x){
y=as.name(x)
ggplot(data=countmatrix, aes(stages, fill = y))+
geom_bar(position="stack", fill="gray34")+
geom_bar(data=subset(countmatrix,y!=0), aes(fill=stages ))
}
x=c("x13","y13")
p2=lapply(x, barplotseries)
do.call("grid.arrange", c(p2, ncol=2))
all want is to be able to make the same plot above on as many variables as possible.
please let me know you suggestions (should I use For?) I don't know how to use it.
thanks
