Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - how do I change font style and size on my two x-axes which I have moved to the top of the graph?

Tags:

r

fonts

size

I am having bother changing the font style and size on my two x-axes, both of which I have moved to the top of the graph.

Here is my code so far:

x1<-Temperature
x2<-Salinity

y<-Depth
par(mar=c(4, 4, 8, 4))

plot(x2,y, type="l",col="darkgrey",ylim=rev(range(0,300)),las=2,xlim=(range(32.5,34.5)),xaxt='n',xlab='',font.axis=2,lwd=3,ylab="Depth [m]",font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
axis(side=3, line=4)
par(new=TRUE)
plot(x1,y, type="l",col="black",ylim=rev(range(0,300)),las=2,xaxt='n',xlab='',lwd=3,ylab='Depth [m]',font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
axis(side=3, line=0)
par(new=TRUE)

This successfully changes my y-axis but leaves my x-axes unchanged.

Help!

like image 521
user1798901 Avatar asked Nov 05 '12 00:11

user1798901


People also ask

How do I change the font size on the X-axis in R?

Go to the menu in RStudio and click on Tools and then Global Options. Select the Appearance tab on the left. Again buried in the middle of things is the font size. Change this to 14 or 16 to start with and see what it looks like.

How do you change the font size on an axes?

To change the text font for any chart element, such as a title or axis, right–click the element, and then click Font. When the Font box appears make the changes you want.

How do I change the size of the X-axis in ggplot2?

To increase the width of the X-axis line for a ggplot2 graph in R, we can use theme function where we can set the axis. line. x. bottom argument size to desired size with element_line.


1 Answers

Reading through the help for axis, ?axis, looking at the documentation for the ... parameters:

 ...: other graphical parameters may also be passed as arguments to
      this function, particularly, ‘cex.axis’, ‘col.axis’ and
      ‘font.axis’ for axis annotation, ‘mgp’ and ‘xaxp’ or ‘yaxp’
      for positioning, ‘tck’ or ‘tcl’ for tick mark length and
      direction, ‘las’ for vertical/horizontal label orientation,
      or ‘fg’ instead of ‘col’, and ‘xpd’ for clipping.  See ‘par’
      on these.

So just pass in your cex.axis etc parameters into the axis call as you did for plot. Here's a reproducible example (Note how I've made up the data, and even though the data is not realistic, at least it makes the example reproducible and still solves your problem):

x1 <- runif(10)
x2 <- runif(10) * 2 + 32.5
y <- runif(10) * 300

par(mar=c(4, 4, 8, 4))    
plot(x2,y, type="l",col="darkgrey",ylim=rev(range(0,300)),las=2,xlim=(range(32.5,34.5)),xaxt='n',xlab='',font.axis=2,lwd=3,ylab="Depth [m]",font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)

# added in various font/axis labels as in above
axis(side=3, line=4,font.axis=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)

par(new=TRUE)
plot(x1,y, type="l",col="black",ylim=rev(range(0,300)),las=2,xaxt='n',xlab='',lwd=3,ylab='Depth [m]',font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
axis(side=3, line=0,font.axis=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)

setting axis properties in R

(Your subsequent calls to axis where replacing the axis from the plot call, so instead of using the axis parameters from plot it uses the axis parameters from axis).

like image 89
mathematical.coffee Avatar answered Oct 15 '22 08:10

mathematical.coffee