1
votes

There is this figure in an economics paper:

enter image description here

I want to style my plots just like these -- with invisible top axis, Y-axis values on the right hand side, axis labels on the top of it and aligned horizontally, subplot titles aligned to the left and each subplot taller than wider and thus emphasizing change along Y-axis. I primarily use MATLAB and I tried to fiddle with it to reproduce something like this but in vain. I then reached out to one of the authors of the paper asking if he could tell which application he used for plots and if he could share how to style my plots after theirs. He responded saying he didn't have the code but he thought it was done in R.

I have not seen plots like this being done in R and even after hours of internet trawling, I didn't find anything R-generated which looked even remotely similar. Will appreciate if you folks have any thoughts/advice on whether it is indeed possible to do it in R.

EDIT

Courtesy of inputs by Isabella Ghement and after whuber's comments, I tried plotting one of the 4 by 2 panels in the figure in my question. Here is how it looks:

enter image description here

Ruefully, this is quite different from the panels in the question. Presumably, if I can get one panel right, I can then prepare a figure containing m by n subplots. That said, this figure has only two elements that I want -- invisible top axis and subplot title aligned to the left. But its tick marks are outside, X-axis doesn't meet the two Y-axes and axis labels are still printed vertically beside them. Here's the code that produces the above plot (taken from https://www.statmethods.net/advgraphs/axes.html, thanks Isabella Ghement for the suggestion):

# specify the data 
x <- c(1:10); y <- x; z <- 10/x
# create extra margin room on the right for an axis 
par(mar=c(5, 4, 4, 8) + 0.1)
# plot x vs. y 
plot(x, y,type="b", pch=21, col="red", 
 yaxt="n", lty=3, xlab="", ylab="", bty="n")
# add x vs. 1/x 
lines(x, z, type="b", pch=22, col="blue", lty=2)
# draw an axis on the left 
axis(2, at=x,labels=x, col.axis="red", las=2)
# draw an axis on the right, with smaller text and ticks 
axis(4, at=z,labels=round(z,digits=2),
 col.axis="blue", las=2, cex.axis=0.7)
# add a title for the right axis 
 mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue")
# add a main title and bottom and left axis labels 
title("(a) Some Variable", xlab="X values",
  ylab="Y=X", adj=0)

I was hoping for simple way of generating such plots but seems like it's a lot of handiwork.

FURTHER EDIT

Though one of the authors wrote back saying he thought plots were in done in R, I do suspect at this point, like @iayork, that probably they weren't done in R. I looked at online appendix of another paper in which one of the authors of the paper in this question is a coauthor and that paper too has plots of similar style. Look at this plot for example:

enter image description here

When I looked at file properties of that plot (it's a PDF file that comes with online appendix in a zipped folder but contains no code), I saw this:

enter image description here

After seeing this, I immediately thought it was generated by S-PLUS but after spending hours on end, I didn't come across anything online on S-PLUS site or any other paper which bore any resemblance. And it was then that I thought it might not be that even though it seems like so by looking at file properties. And then as a last resort, I tried to contact the authors but couldn't get anything useful yet.

1
I believe SO would quickly close this question because it hasn't sufficient detail: no minimal reproducible example, not even sample data. With some fussing, you can generate this figure in base R graphics: no package is needed.whuber
This is an interesting programming question. You will find a good starting point for answering it by studying the R code for "A Silly Axis Example" available at statmethods.net/advgraphs/axes.html. In this example, dedicated to constructing a single one of your type of panels, make sure to modify the plot() command to include the option bty="n" (which will help suppress the top edge of the plot). Furthermore, make sure to modify the title() command to include the option adj=0 (which will help move the plot title to the left).Isabella Ghement
Of course, the strategy outlined in my suggested example will allow you to construct one panel at the time. You can then combine all panels in the same graphical window. I am sorry I can't share the actual R code with you - the question was put on hold, which makes it impossible to add an answer at this stage.Isabella Ghement
@Isabella Presumably at some point this question will be migrated to SO or independently reposted there, so stay tuned!whuber
I just added an MWE to my question. The output from it doesn't go far towards where I want to be.Vivek Sharma

1 Answers

1
votes

You've already deactivated the yaxt, so deactivate xaxt as well. In axis we can omit the labels with labels=FALSE. May I then introduce mtext with which you can create labels independently from the axis. Yes the inward ticks are done with tck=-something. To obtain the axes touch each other we start at 0 and end slightly higher as the maximum value, overlap should be hidden automatically. Add legend. Finally it's wise to use png device to obtain desired aspect ratio. I think that's it, in a nutshell?

# specify the data 
x <- c(0:10); y <- ((x^2)-20)/100; z <- (100/x-3)/100

# helper variables
t.adj <- .03
y.seq <- c(-.5, seq(0, 1.5, length.out=4))

png("ecn.plot.png", width=400, height=500)

# margins
par(mar=c(4, 4, 4, 5) + 0.1)

# plot x vs. y 
plot(x, y,type="l", pch=21, col="red", xlim=c(0, 10), ylim=c(-.42, max(y.seq)),
     yaxt="n", xaxt="n", lty=1, xlab="", ylab="", bty="n", lwd=2)
# add x vs. 1/x 
lines(x, z, type="l", pch=22, col="blue", lty=2, lwd=2)
# add y-zero line
abline(h=0, lwd=2)
# draw axes
axis(1, at=(-1:6)*2,labels=FALSE, col.axis="black", tck=t.adj, lwd=2)
mtext((0:5)*2, 1, 0, at=(0:5)*2,col.axis="black", font=2)
axis(2, at=y.seq, labels=FALSE, col.axis="red", las=2, tck=t.adj, lwd=2)
axis(4, at=y.seq, labels=FALSE, col.axis="red", las=2, tck=t.adj, lwd=2)
mtext(formatC(sort(c(0, y.seq)), digits=1, format="f"), 4, 2, 
      at=sort(c(0, y.seq)), col="black", las=2, font=2, adj=1)
mtext("pct.", 4, 0, at=max(y.seq)+.15, las=2, adj=1, font=2, cex=.9)
# add title
mtext("(a) Some Variable", padj=-2, adj=0, cex=1.2, font=2)
# add legend
legend(x[2], max(y.seq), c("Home", "Foreign"), lty=c(1, 2), 
       col=c("red", "blue"), bty="n", cex=.8)

dev.off()

Result

enter image description here

Ah, for the arranging of multiple plots you may want to look at this answer.