Reputation: 402
I have following example code for producing the box plots for a data frame with categorical variables.
DF<-data.frame("A"=rnorm(20,3,0.2),"B"=rnorm(20,2,0.1),"C"=rnorm(20,2,0.3))
DF<-stack(DF)
BPFunc <- function ( PDataSet,Cat, PData, PTitle,XTitle, YTitle,PlotName) {
arguments <- as.list(match.call())
PData = eval(arguments$PData, PDataSet)
Cat = eval(arguments$Cat, PDataSet)
PDataSet<-data.frame(Cat=Cat,PData=PData)
bp <- ggplot(PDataSet, aes(x = Cat, y = as.numeric( PData) , fill = factor( Cat)))
bp <- bp + geom_boxplot(notch = F)
bp <- bp + labs(title=PTitle, x=XTitle, y =YTitle)
bp <- bp + theme_bw()
bp
}
The above function can be successfully work using:
BPFunc(DF, ind , values ,"My Box","Categories","Value")
Now I want to able my code to save the plot in a jpeg device using following modifications:
BPFunc <- function ( PDataSet,Cat, PData, PTitle,XTitle, YTitle,PlotName) {
arguments <- as.list(match.call())
PData = eval(arguments$PData, PDataSet)
Cat = eval(arguments$Cat, PDataSet)
PDataSet<-data.frame(Cat=Cat,PData=PData)
bp <- ggplot(PDataSet, aes(x = Cat, y = as.numeric( PData) , fill = factor( Cat)))
bp <- bp + geom_boxplot(notch = F)
bp <- bp + labs(title=PTitle, x=XTitle, y =YTitle)
bp <- bp + theme_bw()
jpeg(PlotName, width=4, height=3, units="in", res=600 )
par(mfrow = c(1,1), mgp=c(2, 0.5, 0),oma = c(4,3,4,3) + 0.1, mar = c(3,3,1,1)+0.1,cex=0.5 )
bp
graphics.off()
}
As is my expectation I want from the function to save the plot in PlotName.jpeg file and also I need the plot characteristics can be controlled by par() function. But the produced jpeg device is empty when I call function:
BPFunc(DF, ind , values ,"My Box","Categories","Value","MyPlot.jpeg")
As an alternative I know that I can use print() function as follows:
BPFunc <- function ( PDataSet,Cat, PData, PTitle,XTitle, YTitle,PlotName) {
arguments <- as.list(match.call())
PData = eval(arguments$PData, PDataSet)
Cat = eval(arguments$Cat, PDataSet)
PDataSet<-data.frame(Cat=Cat,PData=PData)
bp <- ggplot(PDataSet, aes(x = Cat, y = as.numeric( PData) , fill = factor( Cat)))
bp <- bp + geom_boxplot(notch = F)
bp <- bp + labs(title=PTitle, x=XTitle, y =YTitle)
bp <- bp + theme_bw()
jpeg(PlotName, width=4, height=3, units="in", res=600 )
par(mfrow = c(1,1), mgp=c(2, 0.5, 0),oma = c(4,3,4,3) + 0.1, mar = c(3,3,1,1)+0.1,cex=0.5 )
print(bp)
graphics.off()
}
But when we use print() the plot characteristics can not be controlled by par() function. in another word par() function will not work with print(). please help me know how I can produce the plot while I can use par() to adjust the plot characteristics? Thanks
Upvotes: 0
Views: 79
Reputation: 17648
Try
BPFunc <- function (PDataSet,Cat, PData, PTitle,XTitle, YTitle,PlotName) {
arguments <- as.list(match.call())
PData = eval(arguments$PData, PDataSet)
Cat = eval(arguments$Cat, PDataSet)
PDataSet<-data.frame(Cat=Cat,PData=PData)
bp <- ggplot(PDataSet, aes(x = Cat, y = as.numeric( PData) , fill = factor( Cat)))
bp <- bp + geom_boxplot(notch = F)
bp <- bp + labs(title=PTitle, x=XTitle, y =YTitle)
bp <- bp + theme_bw()
bp
ggsave(PlotName, width=4, height=3, units="in", dpi=600)
}
BPFunc(DF, ind , values ,"My Box","Categories","Value","MyPlot.jpeg")
Upvotes: 1