Odd Behaviour of breaks_fun Function.

Dear all,

I have been dealing with a problem that is like a mystery to me. I can the following plot with the code below:

# Custom y-axis breaks ~
breaks_fun <- function(x){
  if (min(x) < 1){
    seq(1, 4)}
  else if (min(x) < 83){
    seq(85, 95, by = 5)}
  else if (min(x) < 100){
    seq(96, 99, by = 1)}
  else { 
    seq(0, 40000000, by = 5000000)}}

# Custom y-axis labels ~
plot_index_labels <- 0
labels_fun <- function(x) {
  plot_index_labels <<- plot_index_labels + 1L
  switch(plot_index_labels,
         scales::label_number(accuracy = 0.1, suffix = "X")(x),
         scales::label_percent(accuracy = 1, scale = 1, big.mark = "")(x),
         scales::label_percent(accuracy = 1, scale = 1, big.mark = "")(x),
         scales::label_number(accuracy = 1, big.mark = "", suffix = "M")(x))}

# Creates the panel ~
BSG_Combined <- 
 ggplot() +
  geom_violin(data = fulldfUp, aes(x = Species, y = Value),
              fill = "#ffffff", colour = "#000000", show.legend = FALSE, alpha = .9, size = .3, width = .7) +
  stat_summary(data = fulldfUp, aes(x = Species, y = Value),  
               fun = mean, geom = "point", shape = 21, size = 3.5, alpha = .9, colour = "#000000", fill = "#000000") +
  facet_grid(Category ~. , scales = "free", labeller = labeller(Category = ylabels)) +
  scale_y_continuous(breaks = breaks_fun, labels = labels_fun) +
  theme(panel.background = element_rect(fill = "#ffffff"),
        panel.grid.major = element_line(color = "#ededed", linetype = "dashed", size = .00005),
        panel.grid.minor = element_blank(), 
        panel.border = element_blank(),
        panel.spacing.y = unit(1, "cm"),
        axis.line = element_line(colour = "#000000", size = .3),
        axis.title = element_blank(),
        axis.text.x = element_text(colour = "#000000", size = 16, face = "bold", angle = 45, vjust = 1, hjust = 1),
        axis.text.y = element_text(color = "#000000", size = 16, face = "bold"),
        axis.ticks.x = element_line(color = "#000000", size = .3),
        axis.ticks.y = element_line(color = "#000000", size = .3),
        strip.background.y = element_rect(colour = "#000000", fill = "#d6d6d6", size = 0.3),
        strip.text = element_text(colour = "#000000", size = 20, face = "bold"),
        legend.position = "top",
        legend.margin = margin(t = 0, b = 0, r = 0, l = 0),
        legend.box.margin = margin(t = 10, b = 20, r = 0, l = 0),
        legend.key = element_rect(fill = NA),
        legend.background = element_blank())

So, as you can see the breanks_fun function works for all the facets but for the last one. I have tried to change the function in countless ways -- including adding a final else if[e.g. (min(x) > 1000)] but no combination works. I can also do it without the labels = labels_fun function and the result is the same. I have managed to use this very same function on another data, but it seems that something is going off when I try to apply it on this new data.

Some info:

> summary(TotalReads$Value)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
  576504  6209678 10267860 10715209 14395940 34754853

So the only difference between this data and my previous data is that here the discrepancy between the min and max values is bigger. However, I do not see how this could affect the breanks_fun function since these samples are so different from the ones present in the other 3 categories, hence the else should be more than enough anyway.

Would anyone be able to spot something that I have been missing?
I would very much appreciate any help.

Best regards, George.

1 Like

Hej,

An answer was given here.

Best regards, George.

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.