stacked histogram changes scale when filled in R

111 views Asked by At

I created a stacked histogram with a data of this nature:

    > haps
        tot K7a
    1     1   2
    2     7   1
    3     1   4
    4     8   3
    5     1   6

tot is counts of observations of each element, (I am showing only 5 elements of the 1899 in the table above), K7a is a factor with 7 levels. The data should be read e.g. as: element 1 is observed once and belongs to group 2, ... element 4 is observed 8 times and belongs to group 3, etc.

with the following code I get a histogram in B&W:

      ggplot(haps, aes(x=tot) +
        geom_histogram(binwidth = 0.5) +
        scale_y_continuous(trans="log1p", breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000,                 1500, 2000)) + 
        scale_x_continuous(limits = c(0, 20), breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
        theme_gray(base_size = 10) + labs(x = "Haplotype repeats", y = "Number of events" ) 

enter image description here

Then I want to add colour to the 7 groups, and I use the following code, simply adding fill to aes:

    ggplot(haps, aes(x=tot, fill=K7a)) +
      geom_histogram(binwidth = 0.5) +
      scale_y_continuous(trans="log1p", breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000, 1500, 2000)) + 
      (limits = c(0, 20), breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
      theme_gray(base_size = 10) + labs(x = "Haplotype repeats", y = "Number of events" ) 

enter image description here

the second graph changes the y scale even though I use the same code, I can't figure out how to get a graph with the same scale as in the black & white one.

1

There are 1 answers

0
GGamba On

Not really sure what's happening. My best guess is the transformation is applied only to the y scale and not to the fill scale.
You can 'work around' this issue applying the transformation after all the calculations have been made using coord_trans():

library(ggplot2)

df <- read.table(text="
tot K7a
1     1   2
2     7   1
3     1   4
4     8   3
5     1   6")

ggplot(df, aes(tot, fill = factor(K7a))) +
  scale_y_continuous( breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000,1500, 2000)) +
  geom_histogram(binwidth = 1) +
  coord_trans(y = 'log1p')

Created on 2020-10-18 by the reprex package (v0.3.0)