0

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

4
  • Your reproducible example does not include stages. Commented Oct 29, 2019 at 14:55
  • Save the plot inside your function using a variable so you can access it outside the function. Commented Oct 29, 2019 at 14:59
  • 1
    Also, reproducible example as is does not render such a plot. Please be sure to re-run posted code in an empty R environment (i.e., no previous globalenv objects) to make sure all compiles and renders accordingly. Commented Oct 29, 2019 at 15:09
  • Sorry I forgot to copy the first line. the vector stages is in now. it's reproducible now. Commented Oct 30, 2019 at 15:08

1 Answer 1

1

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 ))
}
Sign up to request clarification or add additional context in comments.

13 Comments

Hey Man! thank you so Much. it worked flawlessly!!! thanks a lot!
what is your subsetting rule now? I hope it is not too complex =p
I want to remove the to bottom 5% of the values (through a Z scoore eliminatation) data=subset(DATA,DATA[,y]>(mean(y))-1.96*sd(y)), but it doesn't work
if I put a value like data=subset(DATA,DATA[,y]> 0 or 1 or 2. it works, but it I put a formula it doesn't (like (mean(y))-1.96*sd(y)))
you have to call DATA[,y] again, because y here is a character.. so do something like data=subset(DATA,DATA[,y]>(mean(DATA[,y]))-1.96*sd(DATA[,y]))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.