logo

How to Draw a Log-Log Scale Plot in R 📂R

How to Draw a Log-Log Scale Plot in R

Not a Good Approach

win.graph(7,4); par(mfrow=c(1,2))
plot(pressure,main='Pressure\')
y<-pressure[-1,]$pressure; logtemp<-log(y)
x<-pressure[-1,]$temperature; logpress<-log(x)
plot(logpress,logtemp,main='log scale\')

1.png The easiest way to draw a log-log scaled graph is to take the log of the data itself. If you are drawing a log-log plot for the first time, it’s definitely worthwhile to get familiar with this method. This approach works with R or any other language, so it can be used in a pinch. However, as much as this method is convenient for the mind, it can be somewhat laborious for the hands.

win.graph(5,5)
plot(pressure,main='Pressure\',log="xy")

2.png To draw a log-log graph using only the native plot() function in R, you can use the log="xy" option. You can choose one of three options: "x", "y", or "xy", and it automatically takes the log of the specified axes for you. However, this method is sufficient for understanding the relationship and trends between two variables, but it’s not particularly attractive. To overcome this, you can use the eaxis() function from the sfsmisc package.

install.packages("sfsmisc")
library(sfsmisc)
 
win.graph(5,5)
plot(pressure,main='Pressure\',log="xy",xaxt='n',yaxt='n')
eaxis(1,at=10^(-4:4))
eaxis(2,at=10^(-4:4))

3.png The eaxis() function literally means exponential axis. It’s not necessary to only include $10^{k}$; if needed, you can insert any desired point values, and it will adjust accordingly. However, before using eaxis(), you need to insert xaxt="n" and yaxt="n" in plot() to remove all original ticks, which results in a clean, desired graph.

install.packages("sfsmisc")
library(sfsmisc)
 
win.graph(7,4); par(mfrow=c(1,2))
plot(pressure,main='Pressure\')
y<-pressure[-1,]$pressure; logtemp<-log(y)
x<-pressure[-1,]$temperature; logpress<-log(x)
plot(logpress,logtemp,main='log scale\')
 
win.graph(5,5)
plot(pressure,main='Pressure\',log="xy")
 
win.graph(5,5)
plot(pressure,main='Pressure\',log="xy",xaxt='n',yaxt='n')
eaxis(1,at=10^(-4:4))
eaxis(2,at=10^(-4:4))