rggplot2

Is there a way to plot x-axis as fractions in ggplot, while using facet_grid?


Essentially I am trying to get my x-axis to present as fractions rather than proportions. Here is my current df

index = c(1:10)
n = c(3,1,2,2,2,4,2,4,4,4)
prop = c(0,0,0,0.5,0.5,0.25,0.5,0,0,0.25)

df <- data.frame(index,n,prop)

Current plot looks like this (I have set scale_x_continuos as such for now, but it is not what I want in the end). This would give me grids separated by n, with the proportion of x in each n.

df %>%
  ggplot(aes(x = prop)) +
  geom_bar(fill = "#D294B2", width = 0.2) +
  facet_grid(~n) +
  labs(title = "Porportion of x in index with 2 or more", x = "Proportion of x", y = "No. of index") +
  geom_text(stat='count', aes(label=..count..), hjust=0.5, vjust=-0.5, size=3) +
  scale_y_continuous(limits = c(0,10)) +
  scale_x_continuous(breaks = seq(from = 0, to = 1, by = 0.5)) +
  theme_bw()

What I would like is for the current proportions to be displayed as fractions instead, with their corresponding n group. Example, where n=2, 0.5 = 1/2 and where n=4, 0.5 = 2/4. I have tried searching for methods to convert my entire table to fractions, but could not find any solutions. I also tried manually renaming the x-axis ticks using scale_x_discrete, but I ended up with an empty x-axis when I added this line

frac_test <- c("0", '0',"1/2","1/3","0","1/4")
scale_x_discrete (labels = frac_test)

At the end, I would like my plot to look something like this, this was manually edited on powerpoint enter image description here

If anyone has any idea, or if it's even possible, I would really appreciate it. I have scoured the internet for a solution but can't find anything that makes sense to me or works for me. The closest I can find is this solution, but I have no idea how the subset works, and it doesn't seem to work for me? I am still quite new to this, so I can't make sense of whats going on.


Solution

  • I assume OP wants different scales per facet, so it might help to use ggh4x::facetted_pos_scales and a function here. I wonder if there's some way to create the list programmatically...

    x_frac <- function(denom) {
      scale_x_continuous(breaks = seq(0, 1, by = 1/denom),
                         labels = paste0(seq(0, denom), "/", denom),
                         # or to get "0" at 0 instead of e.g. 0/3: h/t @Edward for both
                         # labels = MASS::fractions(seq(0, 1, by = 1/denom)),
                         limits = c(0,1))
    }
    
    df %>%
      ggplot(aes(x = prop)) +
      geom_bar(fill = "#D294B2", width = 0.2) +
      labs(title = "Proportion of x in index with 2 or more", x = "Proportion of x", y = "No. of index") +
      geom_text(stat='count', aes(label=..count..), hjust=0.5, vjust=-0.5, size=3) +
      scale_y_continuous(limits = c(0,10)) +
      facet_grid(~n, scales = "free") +
      ggh4x::facetted_pos_scales(x = list(
        n == 1 ~ x_frac(1),
        n == 2 ~ x_frac(2),
        n == 3 ~ x_frac(3),
        n == 4 ~ x_frac(4)
      )) +
      theme_bw()
    

    enter image description here