我正在尝试将R中的两个图组合(使用点阵),箱形图和条形图。我希望盒子和酒吧都是垂直的。对于条形图,我会使用horizontal=FALSE
选项。但是对于箱线图我知道我不能使用那个选项。从this post开始,我将使用水平箱图并将其旋转为垂直。
所以我没有一个旋转图和一个未旋转图,我想要结合起来。这是我试过的:
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)
这是结果。我的条形图也旋转了。我想,因为当我绘制条形图时我没有使用draw.in = "VP"
选项,所以我不会让它旋转。但显然我错了。
有没有办法让下面的条形图无法旋转。
答案 0 :(得分:3)
目前,您正在绘制旋转视口中的两个绘图。您需要在绘制h1之前使用upViewport(),或者在按下视口之前绘制它:
grid.newpage()
print(h1, newpage=FALSE, position=c(0.20, 0.00, 1.00, 1.0), more=TRUE)
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)