Have you been fretting, oh my friends, about the difficulties of making a nice-looking figure in R? Fret no more, because I am here to help.
I think I felt so crummy yesterday because I was coming down with a cold, or maybe a cold-plus. Today I am sniffly and achy and wobbly, but I am optimistic about getting my groove back once my immune system is less overloaded. I'm working on that R&R I told you about, and my revisions table has about 30 items in it. Earlier this week I spent a looooong time on just one of them: making a figure suitable for reproduction in black and white.
One of the things people rave about with R is its graphics. And indeed, you don't have to do very much googling to see beautiful R graphics. Just between you and me and the internet? They are a big pain. The closest I came to throwing over the whole PhD idea was when I had to fix a figure in R. It would have been pretty dumb to quit at that point, since I had successfully defended my dissertation, but I certainly thought about it.
So. You have a figure that awaits your attention? Here's what you do. First put your numbers where R can find them, like so:
> bored.stats <- c(3.2, 1.8)
> bored.nostats <- c(2.8, 4.7)
> sobored.stats <- c(4.8, 1.2)
> unbored.nostats <-c(1.2, 3.8)
For this figure we are going to imagine that your reaction to this post varies, depending on how bored you were when you started reading it and whether or not you have some stats background. Let's suppose that you are rating your before-reading boredom and your after-reading boredom on a 5-point Likert scale, where 1 is SO EXCITED and 5 is CRUSHED BY ENNUI.
If you are planning to save your finished figure, then you need to tell R about your plans at the outset. If you just want to mess around until you get it right, you can skip this step:
> png(filename = "bored.png", height = 300, width = 225, bg = "white")
Now you can start plotting. Lay down the first set of points, without the stupid default labels, and then add your scale and your labels for each axis. Like so:
> plot(bored.stats, ylim = c(0,5), axes = F, ann = F)
> axis(1, at = 1:2, lab = c("Before reading", "After reading")
> axis (2, at = 0:5)
> lines(bored.stats, type = "o")
Hey, presto, you have a start. Now let's add those other lines, with different lines and point markers instead of different colors.
> lines(bored.nostats, type = "o", pch = 22, lty = 2) # plain English: square points, dashed line
> lines(sobored.stats, type = "o", pch = 23, lty = 3) # diamond points, dotted line
> lines(unbored.nostats, type = "o", pch = 24, lty = 4) # triangle points, dashed/dotted line
All of this I figured out with only a modicum of facepalming. The thing that tripped me up was making a legend. Just for you, my friends*, I am sharing the legend knowledge I ferreted out today.
*okay, okay, it's actually for me. Guilty as charged.
Once you tell R what to label your items, like this--
> legend.labels<-c("Bored, stats background", "Bored, no stats background", "Very bored, stats background", "Not bored, no stats background")
--then the following line of code will give you a legend:
> legend(1.15, 1, legend.labels, cex = 0.4, pch = 21:24, lty = 1:4)
Translation: at the point (1.15, 1), use my legend labels to make a small legend (that's the cex = 0.4 part) with the specified sequence of point types and line types.
If you did that optional line of code above so R would save the file, then you need one more line of code at the end:
> dev.off()
If you're not sure where R put your file, just use getwd() to find the directory in which it lurks.
Tada! If it looks like the dog's breakfast, do it on a Mac instead of a Windows box. There's a way to deal with anti-aliasing issues in Windows, but I...am pleased enough that I figured out how to make a legend.
Recent Comments