Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to reverse annotation_logticks #5325

Closed
quantixed opened this issue Jun 16, 2023 · 1 comment · Fixed by #5500
Closed

Add option to reverse annotation_logticks #5325

quantixed opened this issue Jun 16, 2023 · 1 comment · Fixed by #5500
Labels
feature a feature request or enhancement

Comments

@quantixed
Copy link

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)
@teunbrand
Copy link
Collaborator

I'll re-iterate here that I remain of the opinion that this should be captured in a guide instead of an annotation layer.

@teunbrand teunbrand added the feature a feature request or enhancement label Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants