0

I am attempting to make a grouped box plot regarding a dataframe. Example dataframe below:

enter image description here

I would like to make a boxplot with each box for fleas, lice and mites, with each plot separated by age. I'm unsure where to start and would like some guidance. Been trying to look at guides but cant understand where to start.

Bex
  • 1

1 Answers1

1

One of the ways to make boxplots is with package ggplot2. If you wanted a single box, it would be easy - ggplot(df, aes(Fleas)) + geom_boxplot(). If you wanted that stratified by age, it would be ggplot(df, aes(Fleas, Age)) + geom_boxplot().

However you want it to be stratified by both age and insect type. In this case, you can achieve this by reshaping the dataset longer first - see code below.

I created a sample dataset (please next time create a reproducible example like so, so it's easier for people to help you: we can't copy and paste a screenshot of data into R).

# Load libraries
library(ggplot2)
library(tidyr)

# Create a sample dataframe
df <- tibble(
  `Squirrel no.` = 1:100,
  Age = sample(c("Adult", "Juvenile"), replace = T, size = 100),
  Fleas = round(rnorm(100, mean = 7, sd = 5)),
  Lice = round(rnorm(100, mean = 12, sd = 6)),
  Mites = round(rnorm(100, mean = 21, sd = 7))))

# Reshape the dataframe
df %>% pivot_longer(c(Fleas, Lice, Mites)) %>% 
  # Set the key variables
  ggplot(aes(x = value, y = name, fill = Age)) +
  # Specify the type of plot
  geom_boxplot()

Output:

Image

You can customise this rudimental Box plot - plenty of online material on ggplot2.

Andrea M
  • 2,314
  • 1
  • 9
  • 27
  • Hi. I reproduced the data using the code, altering the columns etc. I used the code you suggested based on the data set: df1 <- pivot_longer(df1, c("Fleas", "Lice", "Mites")) ggplot(df1, aes(x = value, y=name, fill = Age)) = geom_boxplot () However, the code came back with no boxplot and only lines, example below. I'm not sure where the error occured. – Bex Mar 27 '22 at 22:33
  • Between the ggplot() line and the geom_boxplot() line you need a `+`, not a `=`. – Andrea M Mar 28 '22 at 08:11