Reputation: 737
This may be silly, but I am not getting how to do it,
What I want?
My function goes like this.
plot_exp <-
function(i){
dat <- subset(dat.frame,En == i )
ggplot(dat,aes(x=hours, y=variable, fill = Mn)) +
geom_point(aes(x=hours, y=variable, fill = Mn),size = 3,color = Mi) + geom_smooth(stat= "smooth" , alpha = I(0.01))
}
ll <- lapply(seq_len(EXP), plot_exp)
do.call(grid.arrange, ll)
and I have two variables Var1, Var2 (Which will be passed through the command line, so cant group it using subset)
I want to run the above function for var1 and var2, my function produces two plots for each complete execution. So now it should produce 2 plots for var1 and two plots for var2.
I just want to know how can I apply the logic here to handle what I want? Thank you
This is what data.frame looks like
En Mn Hours var1 var2
1 1 1 0.1023488 0.6534707
1 1 2 0.1254325 0.5423215
1 1 3 0.1523245 0.2542354
1 2 1 0.1225425 0.2154533
1 2 2 0.1452354 0.4521255
1 2 3 0.1853324 0.2545545
2 1 1 0.1452369 0.2321542
2 1 2 0.1241241 0.2525212
2 1 3 0.0542232 0.2626214
2 2 1 0.8542154 0.2154522
2 2 2 0.0215420 0.5245125
2 2 3 0.2541254 0.2542512
I will table the above data.frame as input and I want to run my function once for var1 and produce two plots and then again run the same function for var2 and produce two more plots, then combine all of then using grid.arrange.
The variable values I have to read from the command line and then I have to do the following to get the required data out of main data frame.
subset((aggregate(cbind(variable1,variable2)~En+Mn+Hours,a, FUN=mean)))
after I read from the commandline and store them inside the "variable1" and "variable2" if I directly call them in the above command its not working. what should I do to enter those two variable values inside the command line.
Upvotes: 0
Views: 117
Reputation: 206197
I made a few changes and ran it on your sample data. Basically i just needed to use aes_string
rather than aes
to allow for a variable with a column name.
myvars<-c("var1", "var2")
plot_exp <- function(i, plotvar) {
dat <- subset(dat.frame,En == i )
ggplot(dat,aes_string(x="Hours", y=plotvar, fill = "Mn")) +
geom_point(aes(color=Mn), size = 3) +
geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess")
}
ll <- do.call(Map, c(plot_exp, expand.grid(i=1:2, plotvar=myvars, stringsAsFactors=F)))
do.call(grid.arrange, ll)
(I'm not sure why the colors of the legends are messed up in the image, they look fine on screen)
For subsetting, use
myvars <- c("var1", "var2")
subset(a[,myvars], a[,c("En","Mn","Hours")], FUN=mean)
Upvotes: 0