0

I'm trying to modify the facet labels in the plot created in this post.

To repeat the code:

library(ggplot2)

mymatrix1 <- matrix(data = 0, nrow = 105, ncol =2)
mymatrix2 <- matrix(data = 0, nrow = 108, ncol =2)
mymatrix3 <- matrix(data = 0, nrow = 112, ncol =2)

mymatrix1[,1] <- sample(0:1, 105, replace= TRUE)
mymatrix1[,2] <- rnorm(105, 52, 2)
mymatrix2[,1] <- sample(0:1, 108, replace= TRUE)
mymatrix2[,2] <- rnorm(108, 60, 2)
mymatrix3[,1] <- sample(0:1, 112, replace= TRUE)
mymatrix3[,2] <- rnorm(112, 70, 2)

mydata <- list(mymatrix1, mymatrix2,mymatrix3)

for(i in 1:3){
    mydata[[i]] <- cbind(mydata[[i]], i)
    colnames(mydata[[i]]) <- c("class", "readcount", "group")
}  

mydata <- as.data.frame(do.call(rbind, mydata))

## fixed scales
p <- qplot(class, readcount, data = mydata, geom="boxplot", fill = factor(class)) +
  geom_jitter(position=position_jitter(w=0.1,h=0.1)) +
  scale_x_continuous(breaks=c(0,1), labels=c("0", "1")) +
  facet_wrap(~ group)

## free scales
p + facet_wrap(~ group, scales = "free")

My goal, in this plot, is to change the facet labels 1, 2 and 3 to something like "a" "b" or "c." I thought that simply changing the data frame column "group" to letters instead of numbers in the for loop:

for(i in 1:3){
    mydata[[i]] <- cbind(mydata[[i]], letters[i])
    colnames(mydata[[i]]) <- c("class", "readcount", "group")
}

would work, but I get this error:

Error: Discrete value supplied to continuous scale.

Any suggestions? I looked at the other posts for changing facet labels, but they didn't seem to work for this type of plot (at least for me).

Thanks so much.

Community
  • 1
  • 1
jhomsy
  • 3
  • 1

1 Answers1

0

Are you looking for something like this?

mydata$group <- as.factor(mydata$group)
levels(mydata$group) <- c("A","B","C")
CMichael
  • 1,856
  • 16
  • 20