3

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.

C. Guff
  • 418
  • 3
  • 18
  • 1
    try replacing the `log2_trans()` call with "log2" – bob1 Oct 19 '18 at 17:04
  • 5
    (As a side comment, a log transformation will be misleading and very hard to interpret for a stacked bar chart. Should two stacked bar segments each equal to 100 be shown as equal heights, or should the top one be 1/2 the size? I'd suggest you switch to a dodged positioning, or figure out a way to avoid showing "composition" on a log scale.) – Jon Spring Oct 19 '18 at 17:05
  • 1
    Without having the dataset for a [reproducible example](https://stackoverflow.com/a/5963610/2359523), it's hard to troubleshoot. – Anonymous coward Oct 19 '18 at 17:39
  • When I remplace `log2_trans()`with "log2" that doesn't work. @bob1 I extended the number of rows (n = 50) for the exemple. @Anonymouscoward – C. Guff Oct 22 '18 at 17:08
  • Can try something like `dput(vesself[1:50,])` to get the first 50 rows in a R readable format - saves a bit of work for others. This is part of the reproducible example people go on about... – bob1 Oct 22 '18 at 22:01
  • Now, the reproductible exemple is in R readable format (coming from the function `dput(vesself[1:50,])` ). – C. Guff Oct 23 '18 at 15:40

1 Answers1

1

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

C. Guff
  • 418
  • 3
  • 18