YBKL
YBKL

Reputation: 25

ggplot subsetting in loop

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 ````

enter image description here 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))

enter image description here 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

Upvotes: 0

Views: 112

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

Along the lines to what @Parfait pointed out, not a good idea to call globalenv objects.

Your original function doesn't work because 1. it takes in a string and you pass it into aes in ggplot2, 2. when you subset on countmatrix, you are passing the string in again..

Make a few edits and below should work:

#keep the data.frame as factors and numeric
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=data.frame(x13,stages,y13)

your edited function

barplotseries <- function(y,DATA){
  DATA_subset = subset(DATA,DATA[,y]>0)      
  ggplot(data=DATA, aes_string(x="stages", fill = y))+
    geom_bar(position="stack",  fill="gray34")+
    geom_bar(data=DATA_subset,  aes(fill=stages ))
}

combining the plots

x=c("x13","y13")
p2=lapply(x,function(i)barplotseries(i,countmatrix))
do.call("grid.arrange", c(p2, ncol=2))

enter image description here

You can also do other types of subsetting, like the y column greater than some cutoff:

barplotseries <- function(y,DATA){
  DATA_subset = subset(DATA,DATA[,y]>mean(DATA[,y])-1.96*sd(DATA[,y]))
  ggplot(data=DATA, aes_string(x="stages", fill = y))+
    geom_bar(position="stack",  fill="gray34")+
    geom_bar(data=DATA_subset,  aes(fill=stages ))
}

Upvotes: 1

Related Questions