I have a little side project that necessitated a function that would plot little flowers on a graph. You could do this by accessing a font that has flowers images but this might not work across systems with different fonts. Instead I wanted a method that would work on any computer using the most basic R functions. The function that I ended up writing is used in the manner of the base points function.

flower <- cex.f="1){</font" cex.x="1," cex.y="1," colx="red" function="" x="" y="">
  petalsx <- -1="" 1="" c="" cex.x="" font="" nbsp="">
  petalsy <- -1="" 0="" c="" cex.y="" font="" nbsp="">
  # this loop prints the petals
  for(i in 1:4){
    points(x + petalsx[i], 
           y + petalsy[i], 
           pch = 16, col = colx, 
           cex = cex.f)
  }
  # this prints the disk
  points(x, y, pch = 16, col = "white", cex = cex.f * .65)

}

Arguments descriptions:
x: position on x axis
y: position on y axis
colx: color of petals
cex.x: spacing of petals on x axis
cex.y: spacing of petals on y axis
cex.f: overall flower size

So lets see how this works:


# set up a base plot
plot(0, 0, col = "white", xaxt = "n", 
     yaxt = "n", xlab = "", ylab = "")
flower(x = 0, y = 0, colx = "red", 
       cex.x = .015, cex.y = .035, cex.f = 1)


That looks pretty good. We had to set the cex.x and cex.y values to very small values because the range of the axes is -1 to 1 in this case.



# this time lets set the axis limits to 1:100
plot(0, 0, col = "white", xaxt = "n", 
     yaxt = "n", xlab = "", ylab = "", 
     xlim=c(0,100), ylim=c(0,100))
# now lets try printing a bunch
x <- replace="T)</font" sample="" size="100,">
y <- replace="T)</font" sample="" size="100,">
fl.col <- font="" viridis::viridis="">
for(i in 1:100){
  flower(x = x[i], y = y[i], 
         colx = fl.col[i], 
         cex.x = 1, cex.y = 1.5, cex.f = 1)
}

This looks more or less like what I was wanting!!


0

Add a comment

Great Blogs
Great Blogs
About Me
About Me
My Photo
I am broadly interested in the application and development of comparative methods to better understand genome evolution at all scales from nucleotides to chromosomes.
Subscribe
Subscribe
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.