Odd Behaviour When log() Transforming Y-axis Scale.

Dear all,

I can create this Histogram with the code below:

ggplot() +
  geom_histogram(data = fulldfUp, aes(x = Value, fill = Pops), colour = "#000000", alpha = .5, bins = 200) +
  scale_x_continuous("5K-Window Fst",
                     breaks = c(.2, .4, .6, .8, 1), 
                     labels = c("0.2", "0.4", "0.6", "0.8", "1.0"),
                     limits = c(0, 1),
                     expand = c(0.005, 0.005)) +
  scale_y_continuous("Frequency",
                     #breaks = c(10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000), 
                     #labels = c("0.01K", "0.1K", "1K", "10K", "100K", "1M", "10M", "100M", "1B", "10B", "100B"),
                     #limits = c(0, 1),
                     expand = c(0, 0)) +
  scale_fill_manual(values = c("#4daf4a", "#377eb8", "#e41a1c")) +
  theme(panel.background = element_rect(fill = "#ffffff"),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 12, color = "#000000"),
        legend.position = "top",
        legend.background = element_rect(fill = FALSE),
        legend.key = element_blank(),
        axis.title.x = element_text(size = 16, face = "bold", color = "#000000", margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y = element_text(size = 16, face = "bold", color = "#000000", margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.text = element_text(size = 11, color = "#000000", face = "bold"),
        axis.ticks = element_line(size = .3, color = "#000000"),
        axis.line = element_line(colour = "#000000", size = .3, color = "#000000")) +
  guides(fill = guide_legend(title = "Comparisons", title.theme = element_text(size = 15, face = "bold"),
                             label.theme = element_text(size = 14)))

For I have values in the y-axis going to up 10K. However, when I try to log transform this scale using trans = "log1p" I get super high values... even though the log1p(10000) should be something like 9.21.

ggplot() +
  geom_histogram(data = fulldfUp, aes(x = Value, fill = Pops), colour = "#000000", alpha = .5, bins = 200) +
  scale_x_continuous("5K-Window Fst",
                     breaks = c(.2, .4, .6, .8, 1), 
                     labels = c("0.2", "0.4", "0.6", "0.8", "1.0"),
                     limits = c(0, 1),
                     expand = c(0.005, 0.005)) +
  scale_y_continuous("Frequency (log1p)",
                     trans = "log1p",
                     breaks = c(10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000), 
                     labels = c("0.01K", "0.1K", "1K", "10K", "100K", "1M", "10M", "100M", "1B", "10B", "100B"),
                     #limits = c(0, 1),
                     expand = c(0, 0)) +
  scale_fill_manual(values = c("#4daf4a", "#377eb8", "#e41a1c")) +
  theme(panel.background = element_rect(fill = "#ffffff"),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 12, color = "#000000"),
        legend.position = "top",
        legend.background = element_rect(fill = FALSE),
        legend.key = element_blank(),
        axis.title.x = element_text(size = 16, face = "bold", color = "#000000", margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y = element_text(size = 16, face = "bold", color = "#000000", margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.text = element_text(size = 11, color = "#000000", face = "bold"),
        axis.ticks = element_line(size = .3, color = "#000000"),
        axis.line = element_line(colour = "#000000", size = .3, color = "#000000")) +
  guides(fill = guide_legend(title = "Comparisons", title.theme = element_text(size = 15, face = "bold"),
                             label.theme = element_text(size = 14)))

Would anyone be so kind as to let me know what might be going on here?

Many thanks in advance, George.

You might try coord_trans(y = "log1p") -- more explanation here and similar answers. I don't have my head wrapped around it yet, but it has to do with the order in which the transformation or the summary are calculated.

1 Like

Thanks very much, @jonspring. I believe that is what I was looking for:

 ggplot() +
  geom_histogram(data = fulldfUp, aes(x = Value, fill = Pops), colour = "#000000", alpha = .5, bins = 200) +
  coord_trans(y = "log1p") +
  scale_x_continuous("5K-Window Fst",
                     breaks = c(.2, .4, .6, .8, 1), 
                     labels = c("0.2", "0.4", "0.6", "0.8", "1.0"),
                     limits = c(0, 1),
                     expand = c(0.005, 0.005)) +
  scale_y_continuous("Frequency (log1p)",
                     breaks = c(10, 100, 1000, 10000), 
                     labels = c("10", "100", "1000", "10000"),
                     limits = c(0, 12000),
                     expand = c(0, 0)) +
  scale_fill_manual(values = c("#4daf4a", "#377eb8", "#e41a1c")) +
  theme(panel.background = element_rect(fill = "#ffffff"),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 12, color = "#000000"),
        legend.position = "top",
        legend.background = element_rect(fill = FALSE),
        legend.key = element_blank(),
        axis.title.x = element_text(size = 16, face = "bold", color = "#000000", margin = margin(t = 20, r = 0, b = 0, l = 0)),
        axis.title.y = element_text(size = 16, face = "bold", color = "#000000", margin = margin(t = 0, r = 20, b = 0, l = 0)),
        axis.text = element_text(size = 11, color = "#000000", face = "bold"),
        axis.ticks = element_line(size = .3, color = "#000000"),
        axis.line = element_line(colour = "#000000", size = .3, color = "#000000")) +
  guides(fill = guide_legend(title = "Comparisons", title.theme = element_text(size = 15, face = "bold"),
                             label.theme = element_text(size = 14)))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.