Search code examples
rggplot2latex

ggplot x-axis ticks for pcc outcome - need symbols and scale_x_discrete not working


I've run a sensitivity analysis using the LHS-PRCC technique for 16 parameters and 10,000 different parameter sets. The outcome is a large pcc. (I'll give a generic example using 100)

df <- cbind(
    pcb = runif(100,0,1),
    pbc = runif(100,0,1),
    psb = runif(100,0,1),
    pbs = runif(100,0,1),
    pci = runif(100,0,1),
    pic = runif(100,0,1),
    psi = runif(100,0,1),
    pis = runif(100,0,1),
    epsilonc = runif(100,0,1),
    epsilons = runif(100,0,1),
    gammac = runif(100,0,1),
    gammas = runif(100,0,1),
    tauc = runif(100,0,1),
    taus = runif(100,0,1),
    sigmab = runif(100,0,1),
    sigmai = runif(100,0,1),
summary.statistic = runif(100,0,1))

bonferroni.alpha <- 0.05/16
PRCC <- pcc(df[ , 1:16], df[ ,17], nboot = 10000, rank = TRUE, conf = 1- bonferroni.alpha)

I'm really struggling with the formatting of the ggplot

plot <- ggplot(PRCC, mapping = aes(), environment = parent.frame(), ylim = c(-1,1)) + ggtitle("PRCC")

This works fine but I want to use symbols and subscript for the x-axis ticks.

I've tried this, but the x-axis ticks just disappear.

plot <- ggplot(PRCC, mapping = aes(), environment = parent.frame(), ylim = c(-1,1)) + ggtitle("PRCC") + 
scale_x_discrete(breaks=c("pcb","pbc","psb","pbs", "pci","pic", "epsilonc","epsilons","gammac","gammas", "tauc","taus", "sigmab", "sigmai"),labels=c((expression(p[cb])), (expression(p[bc])), (expression(p[sb])), (expression(p[bs])), (expression(p[ci])), (expression(p[ic])), (expression(Epsilon[c])),(expression(Epsilon[s])),(expression(Gamma[c])),(expression(Gamma[s])),(expression(Tau[c])),(expression(Tau[s])),(expression(Sigma[b])),(expression(Sigma[i]))))

Solution

  • The issue is that you have a continuous x scale, i.e. you have to use scale_x_continuous to set the labels. For this reason you also have to use numbers for the breaks=. Finally note that you missed two labels.

    library(sensitivity)
    library(ggplot2)
    
    set.seed(123)
    
    bonferroni.alpha <- 0.05 / 16
    
    PRCC <- pcc(
      df[, 1:16], df[, 17],
      nboot = 10, rank = TRUE, conf = 1 - bonferroni.alpha
    )
    
    p <- ggplot(PRCC) +
      ggtitle("PRCC")
    
    p +
      scale_x_continuous(
        breaks = 1:16,
        labels = c(
          expression(p[cb]),
          expression(p[bc]),
          expression(p[sb]),
          expression(p[bs]),
          expression(p[ci]),
          expression(p[ic]),
          expression(p[si]),# Missing = psi
          expression(p[is]),# Missing = pis
          expression(Epsilon[c]),
          expression(Epsilon[s]),
          expression(Gamma[c]),
          expression(Gamma[s]), 
          expression(Tau[c]), 
          expression(Tau[s]), 
          expression(Sigma[b]), 
          expression(Sigma[i])
        )
      )
    

    enter image description here