Home > Enterprise >  geom_jitter box-plot with two different point symbols in R
geom_jitter box-plot with two different point symbols in R

Time:10-01

I am trying to plot a box plot with point data such to show the count of two different fruits, so two box plots for each of them. And I want to change the symbol of the points with respect to their status.

How can I have two different point shapes based on status?

Sample Data

Fruit Condition Status
Apple Ripe      Yes
Mango Unripe    Maybe
Apple Ripe      Yes
Apple Unripe    Maybe
Mango Ripe      Maybe
Mango Unripe    Yes
Mango Ripe      Yes
Apple Ripe      Yes
Apple Unripe    Yes
Apple Unripe    Maybe
Mango Ripe      Yes
Mango Ripe      Yes
Apple Ripe      Yes

Code

library(tidyverse)

# 1st approach
ggplot(df = Food, aes(x= Fruit,
                      y = Condition,
                      color = Fruit))  
  geom_boxplot()   
  geom_jitter(shape=16, position=position_jitter(0.2)) # stuck...

Current result

enter image description here

Desired plot

enter image description here

CodePudding user response:

Unfortunately I don't believe you can plot a boxplot with a binary factor on your y axis, e.g.

library(tidyverse)

Food <- read.table(text = "Fruit Condition Status
Apple Ripe      Yes
Mango Unripe    Maybe
Apple Ripe      Yes
Apple Unripe    Maybe
Mango Ripe      Maybe
Mango Unripe    Yes
Mango Ripe      Yes
Apple Ripe      Yes
Apple Unripe    Yes
Apple Unripe    Maybe
Mango Ripe      Yes
Mango Ripe      Yes
Apple Ripe      Yes",
header = TRUE)

# 1st approach
ggplot(Food, aes(x= Fruit,
                 y = Condition,
                 color = Fruit))  
  geom_boxplot()   
  geom_jitter(aes(shape=Status),
              position=position_jitter(width = 0.2,
                                       height = 0.2))

Created on 2021-10-01 by the reprex package (v2.0.1)

If you had a numeric 'ranking' of condition, e.g. from 1 (unripe) to 10 (ripe) your code works:

library(tidyverse)

Food2 <- read.table(text = "Fruit Condition Status
Apple 9      Yes
Mango 3    Maybe
Apple 7      Yes
Apple 2    Maybe
Mango 9      Maybe
Mango 1    Yes
Mango 10      Yes
Apple 8      Yes
Apple 4    Yes
Apple 2    Maybe
Mango 8      Yes
Mango 9      Yes
Apple 7      Yes",
header = TRUE)

ggplot(Food2, aes(x= Fruit,
                 y = Condition,
                 color = Fruit))  
  geom_boxplot()   
  geom_jitter(aes(shape=Status),
              position=position_jitter(width = 0.2,
                                       height = 0.2))

Created on 2021-10-01 by the reprex package (v2.0.1)

  • Related