How to add a break in X axis? - r

I was wondering whether it is possible to make a plot in R with x axis containing one of the shapes below indicating that x axis is not starting from zero?

How about the axis.break() from the package plotrix? For example:
library(plotrix)
plot(3:10,main="Axis break test")
# put a break at the default axis and position
axis.break()
# or at a certain position
axis.break(axis=1, breakpos=3) # on x-axis position 3

Related

How does R decide the x and y axis of mosaicplot

I am curious how R decides x and y axis of mosaic plot?
data(HairEyeColor)
mosaicplot(HairEyeColor)
It puts the "Hair" at x-axis and "Eye" at the y-axis and break the Sex.
Is it predetermined?
What will happen if we have more than 5 variables and do the mosaic plot?
See: Mosaic Plot vignette
xlab, ylab: x- and y-axis labels used for the plot; by default, the
first and second element of names(dimnames(X)) (i.e., the name of the
first and second variable in X).

Change axis labels with matplot in R

I'm trying to change the x axis in a matplot, but this command doesn't work:
TimePoints=1997:2011
matplot(t(DataMatrix),type='l',col="black",lwd=1,xlab="Anni",ylab="Rifiuti",main="Produzione rifiuti")
axis(side=1,at=TimePoints,labels=TimePoints)
with plot I used this without problems. How can I fix it?
Here you can find the objects: https://dl.dropboxusercontent.com/u/47720440/SOF.RData
I usually do this as follows:
Omit the axes altogether.
Add the axes with desired options one by one.
In R:
# Add argument axes=F to omit the axes
matplot(t(DataMatrix),type='l',col="black",lwd=1,xlab="Anni",ylab="Rifiuti",main="Produzione rifiuti",axes=F)
# Add Y-axis as is
axis(2)
# Add X-axis
# Note that your X-axis range is not in years but in the "column numbers",
# i.e. the X-axis range runs from 1 to 15 (the number of columns in your matrix)
# Possibly that's why your original code example did not work as expected?
axis(side=1,at=1:ncol(DataMatrix),labels=TimePoints)

Set ticks margin on one axis (ggplot2)

When plotting graphs with categorical variables (such as boxplots) with long names, the names have to be shifted using the theme command in ggplot2, then the distance between the axis ticks and the text can be set as well yet this distance is reflected on both axis when it is some time only necessary on one axis. Below some sample code:
df<-data.frame(X=rnorm(50,0,10),Y=c(rep("Some Text",25),rep("Some Larger Text That Takes Space",25)))
#classical boxplots
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45))
#the x axis labels need to be shifted downwards
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45),axis.ticks.margin=unit(4,"cm"))
#now they are shifted but there is unnecessary space on the y-axis
How can we set axis.ticks.margin to act on only one axis?
Try this for example :
library(grid)
axis.ticks.margin=unit(c(4,-4),'cm'))
So, the ggplot2 call becomes:
ggplot(df,aes(x=Y,y=X))+
geom_boxplot()+
theme(axis.text=element_text(size=20),
axis.text.x=element_text(angle=45),
axis.ticks.margin=unit(c(4,-4),'cm'))

R: multiple x axis with annotations

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...

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