Search code examples
rggplot2bar-charthistogramaxis

Log and break in y axis for bar plot (ggplot2)


I have this graph

enter image description here

Code :

library("tidyverse")
library("scales")
#data
> dput(Vesself[1:50,])
structure(list(AREA = c("A10", "A13", "A16", "A2", "A23", "A25", 
"A25", "A26", "A26", "A26", "A27", "A28", "A28", "A36", "A39", 
"A43", "B25", "B25", "B26", "B26", "B30", "B30", "B41", "B43", 
"B44", "C27", "C36", "C7", "D15", "D19", "D24", "D29", "D29", 
"D38", "D51", "E15", "E17", "E18", "E18", "E19", "E19", "E19", 
"E19", "E20", "E27", "E27", "E27", "E28", "E28", "E28"), VESSELm = structure(c(5L, 
5L, 5L, 5L, 5L, 3L, 5L, 5L, 3L, 2L, 5L, 3L, 5L, 3L, 5L, 5L, 5L, 
3L, 3L, 5L, 2L, 5L, 5L, 5L, 5L, 5L, 3L, 5L, 3L, 3L, 3L, 3L, 5L, 
3L, 2L, 5L, 3L, 5L, 3L, 1L, 2L, 5L, 3L, 5L, 3L, 2L, 5L, 2L, 3L, 
5L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
VESSEL = c(1, 1, 1, 2, 1, 2, 5, 5, 2, 1, 1, 1, 6, 1, 1, 5, 
1, 1, 1, 2, 1, 2, 1, 1, 6, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 
1, 6, 1, 3, 1, 1, 1, 1, 1, 5, 1, 22, 2, 1, 8), Clust = structure(c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 
4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor")), row.names = c(NA, 
50L), class = "data.frame")

my_breaksx = c(1, 4, 16, 64, 256, 660)

#Plot

ggHist <- ggplot(data = Vesself, aes(VESSEL, color = Clust, fill = Clust)) + geom_bar(stat = "count", width = 0.08) + scale_color_manual(values = cols, name = "Group") + scale_fill_manual(values = cols, name = "Group") +
  scale_x_continuous(trans = log2_trans(), breaks = my_breaksx) +
  labs(x="Density of ships per area", y="Number of area", title="Distribution of ship density", subtitle="by scales")+
  theme_bw() +
  theme(plot.title = element_text(face="bold", hjust=0.5), plot.subtitle=element_text(hjust=0.5), legend.background = element_rect(fill="grey90", size=0.5, linetype="solid", colour ="black"), aspect.ratio = 1) + 
  facet_wrap(~VESSELm)
ggHist

When I try to apply a logarithm transformation to the y axis, I don't have the same result as the x axis. The values are incredibly high. I don't understand why.

The result of the transformation without manual breaks :

scale_y_continuous(trans = log2_trans())

enter image description here

And the result with manual breaks :

my_breaksy = c(1, 4, 16, 64, 150)

scale_y_continuous(trans = log2_trans(), breaks = my_breaksy)

enter image description here

My goal is to have an equivalent representation as the x axis.


Solution

  • I looked back on the problem and found the solution thanks to that old answer.

    The main problem was, when I was using a logarithmic transformation on geom_bar(stat = "count") with for example the following code:

    scale_y_continuous(trans = log2_trans(),
        breaks = trans_breaks("log2", function(x) 2^x),
        labels = trans_format("log2", math_format(2^.x)))
    

    I reached too high values on the y axis (1073741828 instead of 1000).

    enter image description here

    The solution I used is doing the count and apply a transformation of the output before doing the plot and then plot it with geom_bar(stat = "identity").

    DF <- ddply(Vesself, .(VESSEL, VESSELm, Clust), summarise, n=length(Clust))
    DF$log2n <- log2(DF$n)
    my_breaksy = c(1, 4, 10, 16, 22, 27, 32)
    #Plot
    ggHist <- ggplot(data = DF, aes(x = VESSEL, y =log2n, color = Clust, fill = Clust)) + geom_bar(stat = "identity", width = 0.08) + scale_color_manual(values = cols, name = "Group") + scale_fill_manual(values = cols, name = "Group") +
      scale_x_continuous(trans = log2_trans(), breaks = my_breaksx) +
      scale_y_continuous(breaks = my_breaksy, label = my_breaksy^2) + 
      labs(x="Density of ships per area", y="Number of area", title="Distribution of ship density", subtitle="by scales")+
      theme_bw() +
      theme(plot.title = element_text(face="bold", hjust=0.5), plot.subtitle=element_text(hjust=0.5), legend.background = element_rect(fill="grey90", size=0.5, linetype="solid", colour ="black"), aspect.ratio = 1) + 
      facet_wrap(~VESSELm)
    ggHist
    

    This code giving the expected results

    enter image description here