Skip to content

Instantly share code, notes, and snippets.

@jlehtoma
Created November 29, 2011 14:40
Show Gist options
  • Save jlehtoma/1405022 to your computer and use it in GitHub Desktop.
Save jlehtoma/1405022 to your computer and use it in GitHub Desktop.
R plotting with colorful squares
# Create some data
# Notice that all data is bound to specific range, you will have to adjust the
# polygon coordinates, the empty plot xlim/ylim and ablines according to your
# real data
# Polygon xy coordinates
x1 <- c(0, 2, 2, 0)
y1 <- c(2, 2, 0, 0)
x2 <- c(0, 2, 2, 0)
y2 <- c(4, 4, 2, 2)
x3 <- c(2, 4, 4, 2)
y3 <- c(2, 2, 0, 0)
x4 <- x1 + 2
y4 <- y1 + 2
# Some example points
xp <- rnorm(100, 2, 0.5)
yp <- rnorm(100, 2, 0.5)
# Plot an empty panel
plot(c(4, 4), type="n", ylim=c(0, 4), x=c(4, 0))
# Plot polygons using hex colors, last 2 digits (70) define alpha transparency
# can be adjusted (100=opaque, 0=transparent)
polygon(x1, y1, col="#33CC3370", lty=0)
polygon(x2, y2, col="#FF990070", lty=0)
polygon(x3, y3, col="#FFFF0070", lty=0)
polygon(x4, y4, col="#FF330070", lty=0)
# Plot a vertival and horizontal lines
abline(v=2)
abline(h=2)
# Plot the points (your real numbers go here), if you're plotting somehting else
# than xy coordinates (say, a PCA result object) you may have to adjust the
# plotting syntax
points(xp,yp, pch=16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment