简体   繁体   中英

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; 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. 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 :

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. The second one (in green) is translated by 4 in abscissa and is enlarged by 2 in abscissa and by 5 in ordinate.

在此处输入图片说明

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.

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

And take the output polygon & plot it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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