R: multiple x axis with annotations - r

Is it possible to add more than one x-axis to a plot in R? And to put an annotation next to each scale?
Edit > here's the result of Nick Sabbe idea. For the annotation (a little text at the left of each axis), is it possible ?

You can use the line argument of axis() to place an axis higher or lower, this way you can make multiple axes. With mtext() you can then add a label to the side. Do note that the plot itself is only on one scale so you need to rescale the points and labels of the other scale accordingly:
# Plot and first axis:
plot(1:10,1:10,bty="n",col="red",pch=16,axes=FALSE,xlab="",ylab="")
axis(2,0:11,las=1)
axis(1,0:11,line=1,col="red",col.ticks="red",col.axis="red")
mtext("Label 1",1,line=1,at=0.2,col="red")
# Secondary points and axis:
points(rnorm(10,50,20)/10, rnorm(10,5,2),pch=16, col="blue" )
axis(1,0:11,labels=0:11*10,line=3,col="blue",col.ticks="blue",col.axis="blue")
mtext("Label 2",1,line=3,at=0.2,col="blue")

You can use ?axis for that. Parameter at is in the scale of the original axis of the plot, and you can pass labels to show other values.
You have to scale the axess labels yourself, though.
A very simple/silly example:
plot(1:10,1:10)
axis(side=4, at=c(3,7), labels=c(30,70))
Finally, note that most people consider adding multiple axes to a plot bad form...

Related

How to draw axis in the beggining of the system of equations in R

I would like to draw X and Y axis in the beggining of the system of equations, but i don't know how. When i'm trying to set axis it always putting them under the plotted function.
pi=3.141592
x= seq(-pi,pi,0.1)
plot(x,sin(x), axes=FALSE)
axis (1,-3:3)
I don't wanna put simple lines, because i wanna have values in those axis, and would like to change those values.
Alright!
I get it, to draw Y axis in 0 position i need to add
axis(4, labels=TRUE, pos=0)
after plotting, thanks buddy!
No problem!

Log axis appearance in julia/Plots

Here is a plot generated by julia's Plots library, using the xaxis=:log attribute:
The plot has evenly spaced tick marks, with labels like 10^0.25. This might be useful sometimes, but I find it a bit confusing for this plot, because most people don't know the value of 10^0.25 without looking it up.
I would prefer the x axis to have logarithmically spaced ticks, representing uniform intervals of the quantity on the x axis. Here's a quick example to show what I mean, generated using semilogx in Python's matplotlib lirary:
In this plot the x axis has ticks at x=1, x=2, x=3 etc., which I find more useful for the figures I'm generating. Can this be achieved in julia using Plots?
As it has been told in the discourse topic, the default tick behavior (for most backends) is different than what you expected.
There is a way you can achieve the tick behaviour you want. You can manually set tick positions and tick labels through xticks (yticks or zticks) argument.
For example the following snippet will put ticks at equally spaced points as you wanted in your post.
x = 1:0.1:10;
y = rand(length(x));
plot(x, y, xscale=:log10, xticks=(1:10, 1:10))
The first element of the tuple is for the location of the ticks and the second is for the labels. Although I used a range object for labels, you can use array of Strings or LaTeXStrings etc.
You might also want to take a look at minorticks (xminorticks for your case) and minorgrid attributes in the Julia Plots documentation.

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

get the axis to be the same in the pairs function

pairs(iris[,1:4])
will produce a set of scatter plots relating the 4 variables to each other.
It will scale each axis differently depending on the range of each variable.
Is there a way that I can instead set the axes to be the same for every plot?
You can pass xlim and ylim arguments into pairs():
pairs(iris[,1:4], xlim=c(0,8), ylim=c(0,8))

Rotate X Axis Labels of twoord.plot in R

I am using the twoord.plot function in the plotrix package and need to rotate the X Axis tick labels 45 degrees. Anyone know how to do so?
You need to suppress the usual labeling and put your desired labeling in with text(..., srt=45). Since by default text only goes in the plot region, the y argument may need to be negative, and you will need to extend the plotting region by "lowering" the third argument to usr. This is all described in the R-FAQ.

Resources