How can I plot a second line in a XY-plot, using plot(), to a different scale like this example (purple line)?
My R code for the first (red) line is something like:
p <- sqlQuery(ch,"SELECT wl,param1 FROM qryPlot ORDER BY wl")
plot(p$wl,p$param1,axes=T,xlim=c(400,800),ylim=c(0,100),type="l",col="red")
Here is the general idea:
plot(1:10)
par(new=T)
plot(1:10, rep(50, 10), type='l', axes=F, xlab=NA, ylab=NA)
axis(4)
I slightly extended the answer by #johncolby to this:
x<-1:20
y1<-sqrt(x)
y2<-sqrt(x)*x
plot(x,y1,ylim=c(0,25),col="blue")
par(new=TRUE)
plot(x,y2,ylim=c(0,100),col="red",axes=FALSE)
axis(4)
(axes=FALSE in second plot() command = to prevent labels second axis printed on the left side)
With this result:
Little problem to solve: labels both y-axes are printed to the left side.
Related
There is this figure in an economics paper:
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:
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:
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:
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.
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
Ah, for the arranging of multiple plots you may want to look at this answer.
What is the incatation to create math notation for an axis label of a derivative in R?
Desired output for axis label is:
Tried so far:
plot(0,xlab=expression(delta~y~'/'~delta~x),ylab=expression(dy/dx))
Documentation reviewed:
http://vis.supstat.com/2013/04/mathematical-annotation-in-r/
http://www.astrostatistics.psu.edu/su07/R/html/grDevices/html/plotmath.html
There's seriously no reason why LaTeX markup via MathJax shouldn't be enabled on SO
plot(0, xlab=expression(frac(italic(dy), italic(dx))), ylab=expression(dy/dx))
You might also need to adjust positioning to get it to fit correctly. Compare:
par(mfrow=c(1,2))
plot(0, xlab=expression(frac(italic(dy), italic(dx))), ylab=expression(dy/dx))
plot(0, xlab="", ylab=expression(dy/dx))
mtext(side=1, text=expression(frac(italic(dy), italic(dx))), line=4)
To plot the y-label rotated, it looks like mtext doesn't support the srt (string rotation) parameter, so I guess you have to do it with text. I've placed the label manually, but you could probably do it programmatically by querying the various plot coordinate values.
plot(0, xlab="", ylab="")
mtext(side=1, text=expression(frac(italic(dy), italic(dx))), line=4)
text(0.45,0, labels=expression(frac(italic(dy), italic(dx))), srt=0, xpd=TRUE)
I would like to plot two graphs in the same plotting region with horizontal grid lines. Each side of the grid lines should give the value for one graph or the other. There should be no y-axis.
The grid() function allows me to simply set the number of bins using the ny= argument. How do I get the corresponding labels to the grid lines? Usually, I would use axis(..., lwd=0) to get the labels. However, the function requires label positions with at=c() and does not feature a ny= argument. Is there a way to automatically set the locations from the number of bins?
Based on Miff's hint below, this should solve the problem.
plot(1:10, axes=FALSE, ylim=c(0,10), ylab="")
par(yaxp=c(0, 10, 5))
axis(2, lwd=0, col.axis="gray")
par(new=TRUE)
plot(60:50, axes=FALSE, ylim=c(50,60), ylab="")
par(yaxp=c(50, 60, 5))
axis(4, lwd=0, col.axis="gray")
grid(NA, NULL)
grid() gets its locations for gridlines from axTicks(), which in turn uses numbers from par("yaxp"). If you modify this parameter (rather than explicitly passing it to grid), the result will then apply to both the grid drawn and the axis. For example:
plot(1:10, axes=FALSE)
axis(2) #Default 4 sections between ticks
par(yaxp=c(par("yaxp")[1:2], 7)) #Lets have seven instead
axis(4)
grid() #Grid now matches with right rather than left
Obviously similar works for the x axis.
Struggling with this one. I have 25 data items that I want plotted with bubbles in 5 columns.
The plot can be re-created thus:
xcord <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
ycord <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
zsize <- c(2,1,2,3,6,8,9,1,4,5,5,6,7,8,8,9,5,5,5,5,1,8,1,1,12)
Save the parameters before I change them:
op <- par()
dev.off()
Change the parameters:
par (mfrow=c(1,0), mar=c(2,2,2,2), oma=c(2,2,2,2))
Plot using symbols:
symbols(xcord, ycord, zsize, inches=0.3, fg="white", bg="red", xlab="Events", ylab="Diagnoses", tck=0, xaxt="n", yaxt="n")
mtext("Rheumatic diagnoses by cerebrovasular events", line=2, font=2, cex=1.2)
I am happy with the above plot and deliberately used tck=0, xaxt="n", yaxt="n" to clear the axes. I want to manually overlay custom text, controlled with custom co-ordinates (which work with the sysmbols plot), but have not been able to do it. I have tried some of the par arguments and the axes function.
I also tried leaving the axes on:
symbols(xcord, ycord, zsize, inches=0.3, fg="white", bg="red", xlab="Events", ylab="Diagnoses")
but do not know how to change the output (1,2,3,4,5) to my own custom axes labels.
Thank you.
You are looking for the axis function (see ?axis for details), e.g. to replace the 1:5 with A, B, C, D, E:
axis(side=1, at=1:5, labels=LETTERS[1:5])
For some reason if I try to display data with the following code, I get the axis right, but the actual data does not plot. Any suggestions?
par(bg="lightgray")
adates <-as.Date(row.names(try),format="%Y-%m-%d")
plot(try[,1],x=adates,type="o",axes=FALSE, ann=FALSE)
usr <- par("usr")
rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")
lines(try[,1], col="blue")
axis(2,col.axis="blue",,at=pretty(try[,1]),las=1,labels=sprintf("$%1.0f",pretty(try[,1]/1000)),cex.axis=.75)
axis.Date(1, at=pretty(adates), label=format(pretty(adates),"%y"))
box()
title(main="This is a graph", font.main=4, col.main="red",xlab="Date",ylab="$ (in $1000s)")
Forgetting all the other bits of code, take a look at
plot(try[,1],x=adates,type="o",axes=FALSE, ann=FALSE)
The first argument to plot is the vector x-coordinates, which in this case is try[,1]. You then provide another set of x-coordinates with x=adates. So now you have two sets of x-coords but no y-coords.