1

我正在尝试在 R 中组合两个图表(使用格子),一个箱线图和一个条形图。我希望盒子和栏都是垂直的。对于条形图,我会使用该horizontal=FALSE选项。但是对于箱线图,我知道我不能使用该选项。在这篇文章中,我将使用水平箱线图并将其旋转到垂直。

因此,我没有想要组合的一张旋转图和一张未旋转图。这是我尝试过的:

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"选项,所以我不会让它旋转。但显然我错了。有没有办法让下面的条形图不旋转。

在此处输入图像描述

4

1 回答 1

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)
于 2012-04-28T21:36:39.547 回答