1

I have created the (very simple) boxplot below in R.

boxplot(mpg ~ vs, data = mtcars)
stripchart(mpg ~ vs, data = mtcars,
           method = "jitter",
           vertical = TRUE,
           add = TRUE)

I would then like to do the following, but I have not been able to find an answer while searching around, so I hope someone here might be able to help. I would like to:

  1. Change "1" and "0" to "Yes" and "No", respectively - is that possible without changing the data set?
  2. Change the font to Times New Roman
  3. Highligt one specific jitter dot so that it visually shows the mpg value, for instance the highest value in the vs = 1 group.

Best regards

Peter
  • 11,500
  • 5
  • 21
  • 31
newuser932
  • 21
  • 1
  • Please edit your question to include the additional feature erroneously added as an answer. – Peter Aug 23 '21 at 18:58

3 Answers3

1

For Q1 you can follow @Ben Bolker's answer and for Q2 you can use par and determine the font family you want. Q3 you can do something like below (depending on what you need exactly, since it was not specified in the OP):

par(family = 'serif')
mn.t <- max(mtcars$mpg[mtcars$vs == 1])


boxplot(mpg ~ vs, data = mtcars)
stripchart(mpg ~ vs, data = mtcars,
           method = "jitter",
           vertical = TRUE,
           add = TRUE,
           pch = 0)
points(2, mn.t, col = "orange", 
       pch = 15)

The argument 2 in points stands for vs = 1. enter image description here

patL
  • 2,259
  • 1
  • 17
  • 38
0

For Q1:

boxplot(mpg ~ vs, data = mtcars, names=c("No", "Yes"))

Although depending on what you were doing it might be more principled/better workflow to convert vs to a factor upstream ( <- factor(vs, levels = 0:1, labels = c("No", "Yes")))

For Q2, see Changing Fonts for Graphs in R (extrafonts package, or windowsFont() on Windows)

For Q3, I think you'd probably have to dig around and implement the jittering yourself so that you would know where the jittered point was.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

You could try this which avoids two points for the maximum value:

set.seed(123)

par(family = "serif")

boxplot(mpg ~ vs, data = mtcars,
        names = c("No", "Yes"))

highlight <- row.names(subset(subset(mtcars, vs == 1), mpg == max(mpg)))


stripchart(mpg ~ vs, data = mtcars[row.names(mtcars) != highlight, ],
           method = "jitter",
           vertical = TRUE,
           add = TRUE)

stripchart(mpg ~ vs, data = mtcars[row.names(mtcars) == highlight, ],
           method = "jitter",
           vertical = TRUE,
           col = "red",
           at = 2,
           add = TRUE)

text(2,
     mtcars$mpg[row.names(mtcars) == highlight],
     mtcars$mpg[row.names(mtcars) == highlight],
     cex = 1,
     pos = 2,
     col = "red") 

Created on 2021-08-23 by the reprex package (v2.0.0)

Peter
  • 11,500
  • 5
  • 21
  • 31