2

Sample data test:

   a b       c
1  a x      NA
2  b x 5.1e-03
3  c x 2.0e-01
4  d x 6.7e-05
5  e x 5.1e-03
6  f y 6.2e-05
7  g y 1.0e-02
8  h y 2.5e-03
9  i y 9.8e-02
10 j y 8.7e-04

Say I want to draw a bar graph with this set of data. But because the values differ by several orders of magnitude with each other, it's difficult to appreciably visualise the graph. How do I alter the y-axis in such a way that its values are 0, 10-6, 10-5, 10-4 ... 10-1?

So far I've been trying it with ggplot but I can't get it to work. Thanks!

ggplot(test,
       aes(fill=a,
           y=c,
           x=b)) + 
  geom_bar(position="dodge",
           stat="identity")+
  coord_trans(y="log10")

dput(test): (I don't know the relevance of this but here it is anyway.)

structure(list(a = structure(1:10, .Label = c("a", "b", "c", 
"d", "e", "f", "g", "h", "i", "j"), class = "factor"), b = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("x", "y"), class = "factor"), 
c = c(NA, 0.0051, 0.2, 6.7e-05, 0.0051, 6.2e-05, 0.01, 0.0025, 
0.098, 0.00087)), .Names = c("a", "b", "c"), row.names = c(NA, 
-10L), class = "data.frame")
Dodong
  • 57
  • 1
  • 7

1 Answers1

3

To use log10 scale and reverse bars I used -log(c, 10) within aes(). To use scientific notation on y-axis I used scale_y_continous with scientific format.

library(ggplot2)
ggplot(test, aes(b, -log(c, 10), fill = a)) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_y_continuous(labels = function(x) format(x, scientific = TRUE))

enter image description here

You can rename y-axis to whatever you want with labs(y = ""), for example: labs(y = "-log10 C")

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • Follow-up question: I forgot to mention my real data has some NA values. How do I get around plotting that? Thanks! – Dodong Oct 16 '17 at 08:18
  • @DodongAleta there is function called `na.omit()`, you can remove NA values with that and provide cleaned dataset. If my answer solved your problem you can accept it, thanks :-) – pogibas Oct 16 '17 at 08:20
  • @DodongAleta `test2 <- na.omit(test)` and provide `test2` to `ggplot` instead of `test` – pogibas Oct 16 '17 at 08:22
  • Hmm I'm sorry but it's not the solution I'm looking for. You see, my actual data range from 10-8 to 10-2. I want to graph them in log-scale so that the values won't be too far apart. However, I have several NA (technically zeroes) that I have to show in the graph as zeroes or blank (because it being a zero has relevance in the interpretation). – Dodong Oct 16 '17 at 08:53