0
votes

I would like to be able to add values of a select element to a boxplot.

Example:

library(tidyr)
library(dplyr)
library(ggplot2)

iris$referenceNum = seq(1,length(iris$Species))
iris_Edit = iris %>% 
  pivot_longer(1:4)

p1 <- ggplot(iris_Edit) + 
  geom_boxplot(aes(y=value, fill=name)) +
  facet_wrap(~name,scales="free")
p1

I would like to add the values to a select reference reference number.

Example say you want to add values for the element for reference number "4". Then the values 4.6 would be added to Petal.Length chart, 3.1 to Sepal.Width chart, 1.5 to Petal.Length chart, and 0.2 to Petal.Width chart.

iris_Edit_F = iris_Edit %>% 
  filter(referenceNum  == 4)
iris_Edit_F

How would I go about adding those points to the plot?

1

1 Answers

2
votes

Assign data= to points, and you need to add an x= aesthetic.

(I'll exaggerate this a little with red and large dots.)

p1 +
  geom_point(aes(x = 0, y = value, fill = name),
             color = "red", size = 3,
             data = iris_Edit_F)

ggplot with reference points added