0

I have 2 groups of data (top and bottom) that I have been able to make boxplots for separately, but cannot get them to show in the same graph. I want them side by side for comparison for each gene tested (12 total). So, I would like to have the x-axis labeled with the gene tested, and the y-axis with the ddCt values, with 2 boxplots (1 for the top, 1 for the bottom) for each gene. I have the code for each one separately that works below:

# boxplot of first group
boxplot(Top25[-1], main = "Top Performers Gene Expression Relative to 16S", ylab = "ddCt Values", xlab = "Biofilm Gene", cex.axis = 0.75)

# boxplot of second group 
boxplot(Bottom25 [-1], main = "Bottom Performers Biofilm Gene Expression Relative to 16S", ylab = "ddCt Values", xlab = "Biofilm Gene", cex.axis = 0.75)

Any suggestions for what I may try? I've tried to "melt" them together following another ggplot2 question and got an error saying "object melt was not found". Thanks for any help!

Alysha
  • 35
  • 1
  • 1
  • 11
  • 2
    Can you post a sample of your data? – cmaher Aug 31 '17 at 20:25
  • Possible duplicate of [grouped boxplot r ggplot2](https://stackoverflow.com/questions/36718485/grouped-boxplot-r-ggplot2) – BLT Aug 31 '17 at 20:36
  • Sorry that looks awful. It should be a nice table – Alysha Aug 31 '17 at 20:49
  • Please edit your post to add the data. and use `dput` if possible – emilliman5 Aug 31 '17 at 20:51
  • Essentially, it is the gene tested in each column (12 genes total), with the samples being in the rows. The top samples are in rows 1-46, and the bottom samples are in rows 47-91. – Alysha Aug 31 '17 at 20:53
  • `dput(BoundData)`structure(list(Samples = structure(c(2L, 6L, 7L, 11L, 13L, 14L, .Label = c("1479-SD5", "1711-SA9", "1712-SB4", "2001-SB6", "2028-SD7", "2045-SC1", class = "factor"), icaA = c(22.307, 19.263, 18.586, 17.55, 17.524, .Names = c("Samples", "icaA", "icaB", "icaC", "icaD", "fnbA", "fnbB", "clfA", "clfB", "fib", "ebps", "eno", "cna"), row.names = c(NA, 46L), class = "data.frame") – Alysha Aug 31 '17 at 21:00
  • The dput data was extremely shortened up due to the large output. BoundData is the dataset with both groups of data in it where i used `cbind` to combine Top25 and Bottom 25. I also have the unbound and ungrouped data saved as StaphData. – Alysha Aug 31 '17 at 21:02
  • Please, please, please edit your post instead of commenting... – emilliman5 Aug 31 '17 at 21:18

2 Answers2

0

Here is an example. The main idea is to use add = TRUE to add boxplot to an existing plot and specify the horizontal position of boxplot with at

#DATA
set.seed(42)
top = rnorm(40)
bottom = rnorm(40)

#Create Empty Plot
plot(1, 1, type = "n", xlim = c(0, 3), ylim = range(c(top, bottom)),
     ann = FALSE, xaxt = "n")

#Add boxplots to existing plot
boxplot(x = top, at = 1, add = TRUE)
boxplot(x = bottom, at = 2, add = TRUE)
axis(1, at = c(1, 2), labels = c("top", "bottom"))

enter image description here

d.b
  • 32,245
  • 6
  • 36
  • 77
0

You can do this:

Top25 <- data.frame(gene=1:40, exp=rnorm(40, 2))
Bottom25 <- data.frame(gene=41:80, exp=rnorm(40, -2))

boxplot(list(Top25[, -1], Bottom25[, -1]), names=c("Top25", "Bottom25"))

enter image description here

emilliman5
  • 5,816
  • 3
  • 27
  • 37
  • That works, but I want it to be the gene names where it says Top25 and Bottom 25, with a box plot from the top25 group and the bottom 25 group side by side for comparison. That's what I'm struggling with. – Alysha Aug 31 '17 at 21:15
  • You will need to create a reproducible example with a toy dataset and an example of the output you are expecting before I can get a solution to you. – emilliman5 Aug 31 '17 at 21:28
  • Something like this https://stackoverflow.com/questions/36718485/grouped-boxplot-r-ggplot2 – Alysha Aug 31 '17 at 21:37
  • I'm going through and trying to group my data as they did for care, but as top and bottom. – Alysha Aug 31 '17 at 21:38
  • I keep getting errors with using melt in ggplot. Any alternatives out there? – Alysha Aug 31 '17 at 21:57