[Rd] Mismatch between iris data and symbols in matplot example

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Wed Nov 5 22:18:38 CET 2025


>>>>> Jeffrey Dick     on Wed, 5 Nov 2025 15:54:36 +0800 writes:

    > Setosa sepals are bigger than petals in both length and width:
    > summary(subset(iris, Species == "setosa"))

    >   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   Species
    >  Min.   :4.300   Min.   :2.300   Min.   :1.000   Min.   :0.100  setosa    :50
    >  1st Qu.:4.800   1st Qu.:3.200   1st Qu.:1.400   1st Qu.:0.200  versicolor: 0
    >  Median :5.000   Median :3.400   Median :1.500   Median :0.200  virginica : 0
    >  Mean   :5.006   Mean   :3.428   Mean   :1.462   Mean   :0.246
    >  3rd Qu.:5.200   3rd Qu.:3.675   3rd Qu.:1.575   3rd Qu.:0.300
    >  Max.   :5.800   Max.   :4.400   Max.   :1.900   Max.   :0.600

    > However, in the first example with iris in ?matplot, setosa petals (small red "s") appear to be longer and wider than sepals (big blue "S"). The symbols for versicolor are also swapped. (I'm using R 4.5.2.)

    > A possible fix is to change legend() to put "Sepals" before "Petals" for both species. This ordering corresponds with the column indexing used in the example:

    > iS <- iris$Species == "setosa"
    > names(iris[iS,c(1,3)]) == c("Sepal.Length", "Petal.Length")
    > names(iris[iS,c(2,4)]) == c("Sepal.Width", "Petal.Width")

    > The original legend used bigger (uppercase) symbols for bigger flower parts. Whether or not this was intentional, it is an intuitive choice. Such a congruence can be maintained by changing "sS" to "Ss" and "vV" to "Vv" in all three pch assignments.

    > Here is a modified example with the proposed changes in legend and pch.

    > table(iris$Species) # is data.frame with 'Species' factor
    > iS <- iris$Species == "setosa"
    > iV <- iris$Species == "versicolor"
    > op <- par(bg = "bisque")
    > matplot(c(1, 8), c(0, 4.5), type =  "n", xlab = "Length", ylab = "Width",
    >         main = "Petal and Sepal Dimensions in Iris Blossoms")
    > matpoints(iris[iS,c(1,3)], iris[iS,c(2,4)], pch = "Ss", col = c(2,4))
    > matpoints(iris[iV,c(1,3)], iris[iV,c(2,4)], pch = "Vv", col = c(2,4))
    > legend(1, 4, c("    Setosa Sepals", "    Setosa Petals",
    >                "Versicolor Sepals", "Versicolor Petals"),
    >        pch = "SsVv", col = rep(c(2,4), 2))

    > Regards,
    > Jeff Dick

Thanks a lot, Jeff.

This is an absolute record find in "R history":
This example has been *unchanged* in the matplot.Rd file already

in R's svn rev 2 -- file date  18. Sep 1997
 -- almost 2.5 years before R version 1.0.0

i.e., more than 28 years ago.... and you did properly find an
error, indeed, even though only in the plot labeling.

I will fix it slightly differently than you, keeping the legend
identical, and using slightly more "principled" code....

basically this:

PS <- c("Petal", "Sepal") ; clr <- c(2,4)
psL <- paste(PS, "Length",sep="."); Sps <- paste0("    Setosa ", PS, "s")
psW <- paste(PS, "Width", sep="."); Vps <- paste0("Versicolor ", PS, "s")
matplot(c(1, 8), c(0, 4.5), type = "n", xlab = "Length", ylab = "Width",
        main = "Petal and Sepal Dimensions in Iris Blossoms -- new version" )
matpoints(iris[iS, psL], iris[iS, psW], pch = "sS", col = clr)
matpoints(iris[iV, psL], iris[iV, psW], pch = "vV", col = clr)
legend(1, 4, c(Sps, Vps), pch = "sSvV", col = rep(clr, 2))

---
Thanks again, Jeff!

Martin

--
Martin Maechler
ETH Zurich  and   R Core team



More information about the R-devel mailing list