2

What I am trying to produce is a plot (ggplot2) where two variables are plotted in the same x--axis, but one above and the other bellow the x-axis, but with (1) both up and down being absolute values, and (2) in non-scientific notation. Both tasks can be achieved individually using the functions (1) abs, and (2) comma from the package scales:

## data rlength <- structure(list(Mapping = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("antisense", "sense"), class = "factor"), readlength = c(30L, 35L, 31L, 28L, 34L, 32L, 24L, 22L, 27L, 29L, 25L, 23L, 26L, 33L, 20L, 21L, 26L, 35L, 27L, 28L, 25L, 29L, 32L, 31L, 30L, 34L, 33L, 22L, 23L, 21L, 20L, 24L), N = c(99854L, 7238L, 53523L, 146805L, 8743L, 28161L, 18934L, 6872L, 117191L, 136601L, 35895L, 11110L, 70936L, 14873L, 3807L, 4900L, 139893L, 74330L, 177004L, 173025L, 90852L, 137917L, 76706L, 84552L, 104221L, 72414L, 73268L, 48112L, 51317L, 40638L, 34869L, 64274L)), row.names = c(NA, -32L), class = "data.frame", .Names = c("Mapping", "readlength", "N")) library(plyr) library(ggplot2) library(scales) ## plot1 ggplot(rlength, aes(x=readlength))+ theme_minimal() + geom_bar(subset=.(Mapping == "sense"), aes(y=N), stat="identity", colour="blue", fill="blue")+ geom_bar(subset=.(Mapping == "antisense"), aes(y=-N), stat="identity", colour="red", fill="red")+ geom_hline(yintercept=0, linetype=2)+ scale_y_continuous(labels=abs) ## plot2 ggplot(rlength, aes(x=readlength))+ theme_minimal() + geom_bar(subset=.(Mapping == "sense"), aes(y=N), stat="identity", colour="blue", fill="blue")+ geom_bar(subset=.(Mapping == "antisense"), aes(y=-N), stat="identity", colour="red", fill="red")+ geom_hline(yintercept=0, linetype=2)+ scale_y_continuous(labels=comma) 

But trying to use the 2 together throws an error:

ggplot(rlength, aes(x=readlength))+ theme_minimal() + geom_bar(subset=.(Mapping == "sense"), aes(y=N), stat="identity", colour="blue", fill="blue")+ geom_bar(subset=.(Mapping == "antisense"), aes(y=-N), stat="identity", colour="red", fill="red")+ geom_hline(yintercept=0, linetype=2)+ scale_y_continuous(labels=comma, labels=abs) Error in continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept", : formal argument "labels" matched by multiple actual arguments 

The question is: Is it possible to combine these two formatter functions? If not, an solution on how to produce the plot I am looking for is appreciated.

1 Answer 1

2

I found an answer and it was so obvious. All it is needed is a new function that combines abs and comma:

abs_commma <- function(x) {comma(abs(x))} ggplot(rlength, aes(x=readlength))+ theme_minimal() + geom_bar(subset=.(Mapping == "sense"), aes(y=N), stat="identity", colour="blue", fill="blue")+ geom_bar(subset=.(Mapping == "antisense"), aes(y=-N), stat="identity", colour="red", fill="red")+ geom_hline(yintercept=0, linetype=2)+ scale_y_continuous(labels=abs_commma) 

I am putting it here for future reference.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I'd just worked it out too! Opens up interesting possibilities for writing all sorts of label functions...
Indeed it does @MhairiMcNeill. I was aware that one could create our own formatting functions, but this is so very simple.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.