I saw the ideal tick-mark structure for a log="y"
plot in this paper, Figure 3b 3c 3d.
It has short, log-spaced minor tick marks without labels, plus long, log-spaced major tick marks with labels.
Does anyone know how to achieve this in R
?
This can be done easily using the ggplot2 functions scale_x_continuous() and scale_y_continuous(), which make it possible to set log2 or log10 axis scale. An other possibility is the function scale_x_log10() and scale_y_log10(), which transform, respectively, the x and y axis scales into a log scale: base 10.
To hide or to show tick mark labels, the following graphical parameters can be used : xaxt : a character specifying the x axis type; possible values are either “s” (for showing the axis) or “n” ( for hiding the axis)
To create a Log-Log plot in base R we pass log(data) as data argument instead of data in the plot() function. The log() function converts the data value into its logarithmic value.
Here is a ggplot2
solution:
library(ggplot2)
set.seed(20180407)
df = data.frame(x = seq(from = 1, by = 1, length.out = 20),
y = 2^(seq(to = 1, by = -1, length.out = 20) + rnorm(20, 0, 0.7)))
ggplot(data = df, aes(x = x, y = y)) +
geom_line() +
scale_y_log10() +
annotation_logticks(sides = "l")
You can make it look even more than the paper you linked to with some theming:
ggplot(data = df, aes(x = x, y = y)) +
geom_line(colour = "blue") +
geom_point(colour = "blue") +
scale_y_log10() +
annotation_logticks(sides = "l") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.line = element_line(),
axis.ticks.x = element_line())
In base R just build the axes however you want. Something like this could be a start.
set.seed(5)
d <- data.frame(x=1:100, y=rlnorm(100, meanlog=5, sdlog=3))
with(d, {
plot(x, y, log="y", yaxt="n")
y1 <- floor(log10(range(y)))
pow <- seq(y1[1], y1[2]+1)
ticksat <- as.vector(sapply(pow, function(p) (1:10)*10^p))
axis(2, 10^pow)
axis(2, ticksat, labels=NA, tcl=-0.25, lwd=0, lwd.ticks=1)
})
In lattice
, the latticeExtra
package has the capability:
library(lattice)
library(latticeExtra)
xyplot(y~x, data=d, scales=list(y=list(log=10)),
yscale.components=yscale.components.log10ticks)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With