Pumpkin Carving with R!

Figure 1. A virtual pumpkin, carved using the R function “polygon”.

You can carve your very own virtual pumpkin with R.

Just copy and paste the R script below into your R console. Press “enter”. A graphics window with a pumpkin should appear.

Now click on the pumpkin. A “+” will appear in the graphics window.

Click on 3 points on the graphics window to specify the corners of a triangle. Nothing will happen in the graphics window until you have clicked on 3 points. This will remove the first piece from the pumpkin. You get a total of 4 triangles to carve your pumpkin.

At any time you can close the graphics window to exit your pumpkin carving session.

Have fun!

R Script

plot(c(0,0), cex=0, xlim=c(-1,1), ylim=c(-1,1))
X <- runif(100,-1,1)
Y <- runif(100,0,1)
M <- rchisq(100,1)/20
points(Y~X, cex=M, pch=19, col=colors()[1])
points(0,-0.5, pch=19, col=colors()[498],cex=20)
for (i in 1:4){polygon(locator(3),col=7)}


2 thoughts on “Pumpkin Carving with R!

  1. I made a pumpkin! Which part of the code alters the number and shape of the drawings? If, for example, I’d like to make a pumpkin pi, it looks like it is that last line, “for (i in 1:4){polygon(locator(3),col=7)}”, but does the locator(3) indicate the number of points in the polygon, and the (i in 1:4) indicates the number of polygons to draw? If that is the case, I’d switch to just have one polygon (the pi symbol) with something like 20 drawing points, so would I write “for (i in 1:1){polygon(locator(20),col=7)}”?

    • You’ve got the right idea.

      In the command “for (i in 1:n) {polygon(p),col=7}”, n is the number of polygons, and p is the number of points in the polygon.

      The ‘7’ indicates the colour of polygons. Alternately, you can write ‘ col=”yellow” ‘

      For a list of colours for the crust and filling of your pumpkin pi, you can enter ‘colours()’ into the R console command line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s