Search code examples
rlabelstripchart

add data labels to stripchart


I have made a stripchart with a threshold marked in red. I would like to label the point that falls to the left of the threshold, but can't seem to get the 'text' function working at all.

stripchart screenshot

Here is the stripchart code: stripchart(ctrls$`Staining Green`, method="jitter", pch=4, xlab='Staining Green', cex.lab=2) abline(v=5,col=2,lty=3)

I first tried to filter only those samples below the threshold: Staining.Green <- filter(QCcontrols, Staining.Green < 5) then adding the text with text(Staining.Green$`Staining Green` + 0.1, 1.1, labels = Staining.Green$Sample_Name, cex = 2) This didn't add any text to the chart.

Then I tried labeling all the points, in case I was making it too complicated, with variations on: text(ctrls$`Staining Green` + 0.1, 1.1, labels = ctrls$Sample_Name) Again, no text added, and no error message.

Any suggestions greatly appreciated!

Update: my ctrls object is more complex than I realized - maybe this is tripping me up:

List of 17
 $ Restoration                 : num [1:504] 0.0799 0.089 0.1015 0.1096 0.1092 ...
  ..- attr(*, "threshold")= num 0
 $ Staining Green              : num [1:504] 25.1 23.5 21.1 19.7 22.3 ...
  ..- attr(*, "threshold")= num 5
 $ Staining Red                : num [1:504] 39.8 40.9 36.9 33.2 33.2 ...
  ..- attr(*, "threshold")= num 5.......```

Solution

  • Here is one example using the built in data set for airquality:

    stripchart(airquality$Ozone,
               main="Mean ozone in parts per billion at Roosevelt Island",
               xlab="Parts Per Billion",
               ylab="Ozone",
               method="jitter",
               col="orange",
               pch=4
    )
    
    abline(v = 5, col = 2, lty = 3)
    
    with(subset(airquality, Ozone < 5), text(Ozone, 1.1, labels = Ozone))
    

    Plot

    example stripchart with text labels

    Data

    The lowest values of Ozone are:

    head(sort(airquality$Ozone), 5)
    [1] 1 4 6 7 7
    

    Edit:

    Here's a quick demo with a list with a similar structure:

    vec1 <- c(0.0799, 0.089, 0.1015, 0.1096, 0.1092)
    attr(vec1, 'threshold') <- 4
    
    vec2 <- c(25.1, 3, 21.1, 19.7, 22.3)
    attr(vec2, 'threshold') <- 5
    
    ctrls <- list(Restoration = vec1, `Staining Green` = vec2)
    
    stripchart(ctrls$`Staining Green`, 
               method="jitter", 
               pch=4, 
               xlab='Staining Green', 
               cex.lab=2
    )
    
    abline(v=5,col=2,lty=3)
    
    text(ctrls$`Staining Green`[ctrls$`Staining Green` < 5], 1.1, labels = ctrls$`Staining Green`[ctrls$`Staining Green` < 5])
    

    Note: Instead of explicitly including 5 for threshold you can substitute the threshold from your list attribute:

    attr(ctrls$`Staining Green`, "threshold")
    [1] 5
    

    Plot

    stripchart