I am trying to combine two graphs in R (using lattice), a boxplot and a barchart. I want both the box and the bar to be vertical. For the barchart, I would use the horizontal=FALSE option. But for the boxplot I know I cannot use that option. From this post, I will use the horizontal boxplot and rotate it to vertical.
So not I have one rotated graph and one un-rotated graph, that I want to combine. Here is what I tried:
library(lattice)
mydata <- data.frame(
methods = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),
savings = c(38.1677115678,40.0271480111,3.0658967834,8.55162576,54.5599157619,52.5120636113,6.0234897727,8.1064331259,63.9865435108,64.7867073871,3.8590732507,7.7348316433,49.7059967478,53.8979070884,2.497892375)
)
attach(mydata)
png("test.png", width=800, height=600)
h1 = barchart( savings ~ methods, data=mydata, horizontal=FALSE,
ylab = "", main = "Predicted Savings",
scales=list(y=list(labels=c("","","","","","","","","","","","","",""),tick.number=10))
)
require(grid)
grid.newpage()
pushviewport(viewport(angle = 90, name = "VP"))
h2 = bwplot(savings, xlim=c(0,100),
xlab=list(label="predicted savings", fontsize=14),
scales = list(x = list(tick.number=10)), draw.in = "VP"
)
print( h2,newpage=FALSE, position=c(0.15, 0.95, 0.85, 1.2), more=TRUE)
print(h1, newpage=FALSE, position=c(0.20, 0.00, 1.00, 1.0), more=TRUE)
And here is the result. I got the barchart also rotated. I thought, since I do not use draw.in = "VP" option when I draw the barchart, then I will not get it rotated. But apparently I am wrong.
Is there a way I can get the barchart below un-rotated.

Currently you are plotting both plots in the rotated viewport. You need either to go upViewport() before plotting h1, or simply plot it before pushing the viewport: