Ignore sign of numbers with ggplot2

I have the following df:

gene = c("a", "b", "c", "d")
fc = c(-1, -2, 1, 2)
df = data.frame(gene, fc)

I am using the following code for plotting:

ggplot(df, aes(gene, fc)) + geom_point(size=df$fc) + theme_minimal()

How can I ignore the sign of the values in "fc" while plotting?

Thanks

1 answer

  • answered 2018-10-11 20:41 MrFlick

    You can use the absolute value function abs() to ignore the negative sign. For example

    ggplot(df, aes(gene, fc)) + 
      geom_point(aes(size=abs(fc))) + 
      theme_minimal()
    

    Just make sure to put properties that you want to map to data inside aes() at all times. Rarely should you ever see a $ in ggplot code.