1

I am fairly new to R and want to combine 2 basic box plots into 1.

My code so far is...

library(tidyverse)
library(vegan)
spiders <- read_csv(file = "Spider Data.csv")
class(spiders)
str(spiders)
boxplot(spiders$Length_mm ~ spiders$Distance)
boxplot(spiders$Length_mm ~ spiders$Cover)

Here are the plots I have so far as well as something like what I want to create (ignore the label names in the coloured graph, I just took the picture from google, but I want something similar)

my first boxplot

enter image description here

my second boxplot

enter image description here

what I want my graph to look like (ignore labels)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Dana Martin
  • 11
  • 1
  • 2
  • 1
    Hi, welcome to SO. Please consider using reproducible data (e.g., from a dataset like `iris`, rather than a csv file on your computer). [This post has some great info for how to write a good question.](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – heds1 Aug 19 '19 at 20:47
  • Try: http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/ – MAPK Aug 19 '19 at 20:51

3 Answers3

0

If you wan tto stay with base R, and not other plot library, you can use at= in boxplots to say where the boxes should be displayed and col= to set different colors. This would give for example:

library(tidyverse)
library(vegan)
spiders <- read_csv(file = "Spider Data.csv")
class(spiders)
str(spiders)
boxplot(spiders$Length_mm ~ spiders$Distance, at=c(0.85,1.85), col=2, names="Good")
boxplot(spiders$Length_mm ~ spiders$Cover, at=c(1.15,2.15), col=3, names="Bad")
Chelmy88
  • 1,106
  • 1
  • 6
  • 17
0
boxplot(spiders$Length_mm ~ spiders$Distance+spiders$Cover)

Have a look at How do I make a boxplot with two categorical variables in R?

Fabio Natalini
  • 197
  • 2
  • 2
0

You can use ggplot library will the fill parameter:

ggplot(data = spiders, aes = (x = Distance, y = Length_mm, fill = Cover) +
   geom_boxplot()

You can see the documentation here and this usefull web site r-graph-gallery.

Hope it's hepl!

Adrien Riaux
  • 266
  • 9