简体   繁体   English

R绘制钻石并调整其宽度和高度

[英]R plot diamond and adjust its width and height

I want to plot diamonds, using different values for width and height. 我想使用不同的宽度和高度值来绘制菱形。

In this easy example I want to draw three diamonds, the first (grey) shall be not wide but high, the second (red) as wide as high and the third wide but not high. 在这个简单的示例中,我想绘制三颗菱形,第一个(灰色)宽度不高,第二个(红色)宽度不高,第三个(红色)宽度不高。

I prepared a very simple R code for a plot to illustrate this; 我为绘图准备了一个非常简单的R代码来说明这一点。 I added a vector width and height to illustrate what I want. 我添加了一个矢量宽度和高度来说明我想要的。 cex uses in my example only the first three parameters to enlarge the diamonds. 在我的示例中, cex仅使用前三个参数来放大菱形。 Is there a possibility to form the diamond (maybe with another package?) with different height and width? 是否有可能形成不同高度和宽度的钻石(也许与另一个包装一起?)? Or is there an easy way to adjust my code? 还是有一种简单的方法可以调整我的代码?

outfilename="diamondsize.png"
png(file=outfilename)

xCoords=c(1,2,3)
yCoords=c(1,2,3)
width=c(5,10,15)
height=c(15,10,5)
plot(xCoords,yCoords,pch=23,cex=cbind(width,height),
     bg=c("grey","red","navyblue"),xlim=c(0,4),ylim=c(0,4))
dev.off()

Thank you very much! 非常感谢你!

This is a sample of what can be done with polygon : 这是可以用polygon完成的示例:

x1 <- c(1,2,3,2,1)
y1 <- c(0,1,0,-1,0)

x2 <- 2*x1+4
y2 <- 5*y1
plot(c(x1,x2),c(y1,y2),type='n');
polygon(x1,y1,col=2,border=2)
polygon(x2,y2,col=3,border=3)

This creates 2 diamonds. 这将产生2颗钻石。 The second one (in green) is translated by 4 in abscissa and is enlarged by 2 in abscissa and by 5 in ordinate. 第二个(绿色)在横坐标上转换为4,在横坐标上放大为2,在纵坐标上放大为5。

在此处输入图片说明

It's a matter of taste, but I would create a "default diamond" object like diamond <- cbind(c(-1,0,1,0),c(0,1,0,-1)) and then create a function to re-size it. 这是一个品味问题,但是我会创建一个“默认钻石”对象,例如diamond <- cbind(c(-1,0,1,0),c(0,1,0,-1)) ,然后创建一个功能调整大小。

dsize<-function(x,y,diamond=diamond) { 
    diamond[,1]<-diamond[,1]*x
    diamond[,2]<-diamond[,2]*y
}

And take the output polygon & plot it. 并获取输出多边形并将其绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM