Closed
Description
This is a renewal of issue #3822 which was closed due to lack of reprex and detail.
Sometimes a log scale needs to be shown in reverse. For example, when creating a volcano plot, the y-axis shows a -log10 representation of the p-value.
I would like to be able to add log ticks to the axis and have them correspond to the axis. Right now, annotation_logticks()
only works in one direction and not in reverse. An option reverse = 1
would be very useful.
library(ggplot2)
df <- read.table(header = TRUE, text = "
p ratio
1 0.28801033 3.7712035
2 0.48073187 0.8304503
3 0.06113270 0.7417715
4 0.96118760 1.7041385
5 0.07617357 0.7598174
6 0.15079531 0.7283150
7 0.29093358 0.8806807
8 0.12198144 0.5412664
9 0.18261693 1.4875495
10 0.36864248 1.0625448
11 0.04297883 72.6147534
12 0.01377004 32.0841697")
df$minus_log10_pvalue <- -log10(df$p)
df$log2_fc <- log2(df$ratio)
ggplot(df, aes(x = log2_fc, y = minus_log10_pvalue)) +
geom_point() +
scale_x_continuous(limits = c(-max(abs(df$log2_fc)),max(abs(df$log2_fc)))) +
scale_y_continuous(limits = c(0,3), labels = function(i) 10^-i) +
annotation_logticks(sides = "l", outside = TRUE) +
coord_cartesian(clip = "off") +
labs(x = "Fold change (log2)", y = "P-value") +
theme_classic(9)
The output shows the nine intervening ticks between the major ticks being bunched towards the smaller rather than larger value. The same result is seen using the original data and the appropriate trans
arguments for scale_y_continuous()
library(scales)
ggplot(df, aes(x = log2_fc, y = p)) +
geom_point() +
scale_x_continuous(limits = c(-max(abs(df$log2_fc)),max(abs(df$log2_fc)))) +
scale_y_continuous(trans = c("log10", "reverse")) +
annotation_logticks(sides = "l", outside = TRUE) +
coord_cartesian(clip = "off") +
labs(x = "Fold change (log2)", y = "P-value") +
theme_classic(9)