如何使用网格包绘制矩形 [英] How to draw rectangle using grid package

查看:116
本文介绍了如何使用网格包绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在图表上绘制一个矩形以突出显示不同的更改.我需要使用grid包.我试图使用grid.rect,但是它不起作用.我希望我的矩形看起来像在图片上.

I need to draw a rectangle on my diagram to highlight different changes. I need to use grid package. I tried to use the grid.rect but it doesn't work. I want that my rectangle looks like on the picture.

在图片的左侧,您可以看到我的图表,在图片的右侧,我添加了矩形(在Paint中),就像我想要的那样.

On the left part of the picture you can see my diagram and of the right part of the picture I've added the rectangle (in Paint) like I want it will be.

library(grid)
library(lattice)
library(sandwich)

data("Investment")
Investment <- as.data.frame(Investment)

trellis.par.set(theme = canonical.theme("postscript", color=FALSE))
grid.newpage()
pushViewport(viewport(x=0, width=.4, just="left"))
print(barchart(table(Investment$Interest)),
  newpage=FALSE)
popViewport()
pushViewport(viewport(x=.4, width=.5, just="left"))
print(xyplot(Investment ~ Price, data=Investment, 
         auto.key=list(space="right"),
         par.settings=list(superpose.symbol=list(pch=c(1, 3, 16),
                             fill="white"))),
  newpage=FALSE)

popViewport()

推荐答案

目前尚不清楚您要在哪个位置绘制矩形,但是下面的代码将添加矩形以大致匹配您的图片.您可以调整位置.

It is not completely clear where you are trying to draw the rectangle, but the code below will add the rectangle to approximately match your picture. You can tune the position.

按原样使用您的代码.我将从重复您的打印语句开始,然后添加矩形.

Use your code just as you had it. I will start by repeating your print statement and then adding the rectangle.

print(xyplot(Investment ~ Price, data=Investment, 
         auto.key=list(space="right"),
         par.settings=list(superpose.symbol=list(pch=c(1, 3, 16),
                             fill="white"))),
  newpage=FALSE)


grid.rect(x = unit(0.42, "npc"), y = unit(0.35, "npc"),
          width = unit(0.2, "npc"), height = unit(0.2, "npc"),
        gp=gpar(col="red"))

popViewport()

这篇关于如何使用网格包绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆