0

I'm currently trying to develop a similar result as this link. I have a significant number of columns and several different labels for the x-axis.

col1 <- c(2, 4, 1, 2, 5, 1, 2, 0, 1, 4, 4, 3, 5, 2, 4, 3, 3, 6, 5, 3, 6, 4, 3, 4, 4, 3, 4, 
          2, 4, 3, 3, 5, 3, 5, 5, 0, 0, 3, 3, 6, 5, 4, 4, 1, 3, 3, 2, 0, 5, 3, 6, 6, 2, 3, 
          3, 1, 5, 3, 4, 6)
col2 <- c(2, 4, 4, 0, 4, 4, 4, 4, 1, 4, 4, 3, 5, 0, 4, 5, 3, 6, 5, 3, 6, 4, 4, 2, 4, 4, 4, 
          1, 1, 2, 2, 3, 3, 5, 0, 3, 4, 2, 4, 5, 5, 4, 4, 2, 3, 5, 2, 6, 5, 2, 4, 6, 3, 3, 
          3, 1, 4, 3, 5, 4)
col3 <- c(2, 5, 4, 1, 4, 2, 3, 0, 1, 3, 4, 2, 5, 1, 4, 3, 4, 6, 3, 4, 6, 4, 1, 3, 5, 4, 3, 
          2, 1, 3, 2, 2, 2, 4, 0, 1, 4, 4, 3, 5, 3, 2, 5, 2, 3, 3, 4, 2, 4, 2, 4, 5, 1, 3, 
          3, 3, 4, 3, 5, 4)
col4 <- c(2, 5, 2, 1, 4, 1, 3, 4, 1, 3, 5, 2, 4, 3, 5, 3, 4, 6, 3, 4, 6, 4, 3, 2, 5, 5, 4,
          2, 3, 2, 2, 3, 3, 4, 0, 1, 4, 3, 3, 5, 4, 4, 4, 3, 3, 5, 4, 3, 5, 3, 6, 6, 4, 2, 
          3, 3, 4, 4, 4, 6)
data2 <- data.frame(col1,col2,col3,col4)
data2[,1:4] <- lapply(data2[,1:4], as.factor)
colnames(data2)<- c("A","B","C", "D")

> x.axis.list

[[1]]
expression(beta[paste(1, ",", 1L)])

[[2]]
expression(beta[paste(1, ",", 2L)])

[[3]]
expression(beta[paste(1, ",", 3L)])

[[4]]
expression(beta[paste(1, ",", 4L)])

myplots <- vector('list', ncol(data2))

for (i in seq_along(data2)) {
    message(i)
    myplots[[i]] <- local({
        i <- i
        p1 <- ggplot(data2, aes(x = data2[[i]])) +
            geom_histogram(fill = "lightgreen") +
            xlab(x.axis.list[[i]])
        print(p1)
    })
}

In the past, I've been able to do something similar to this where I can just put x.axis.list[[i]] in my loop and change the symbols. However, I continue to get the term expression on the axis. So the symbol for Beta is correct as well as the subscript but the word "expression" remains. I'm not sure exactly what I'm doing wrong, for a moment, I was able to produce a plot without "expression" but it has since stayed in the ggplot.

I want to be able to produce this plot, or one with the title on the y-axis without the word "expression".

My image currently looks like this. I'm not worried about this example data and the result of the plot, I'm wondering how to get rid of "expression" so only the math symbol shows.

Thanks in advance.

AW27
  • 481
  • 3
  • 15
  • I am not exactly sure of what you want, can you write an x-axis label exactly as you want ? – Santiago I. Hurtado Dec 01 '20 at 12:17
  • I've added an updated example output. I don't understand why "expression" prints on the plot axis. – AW27 Dec 01 '20 at 12:20
  • The problem seems to appear when I loop through the list of expressions. When I put a single expression in, I can get the output I am looking for, unfortunately, I want to update the axis for several plots. – AW27 Dec 01 '20 at 12:32

1 Answers1

2

You can do:

for (i in seq_along(data2)) {
    df <- data2[i]
    names(df)[1] <- "x"
    myplots[[i]] <- local({
        p1 <- ggplot(df, aes(x = x)) +
            geom_bar(fill = "lightgreen", stat = "count") +
            xlab(x.axis.list[[i]])
    })
}

And we can show all the plots together:

library(patchwork)
(myplots[[1]] + myplots[[2]]) / (myplots[[3]] + myplots[[4]])

enter image description here

Note I created the expression list like this:

x.axis.list <- lapply(1:4, function(i){
  parse(text = paste0("beta[paste(1, \",\", ", i, ")]"))
  })
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • How can I flip the numbers? so have `i` before the 1 in ```x.axis.list <- lapply(1:4, function(i){ parse(text = paste0("beta[paste(1, \",\", ", i, ")]")) })``` – AW27 Dec 01 '20 at 15:08
  • 1
    @AW27 you can do `parse(text = paste0("beta[paste(", i, ", \",\", 1)]"))` – Allan Cameron Dec 01 '20 at 15:10