I bought some M&M’s and Skittles while I was on vacation in New York in summer 2025 and quickly forgot about them. By Halloween, I was getting ready to show my labmates how to analyze data in R at our next lab meeting. I brought the candy, explained what I had in mind, and we counted the pieces by color. We were used to collecting data together in streams. This time, we got to eat what we counted.

My advisor had taught us how to use PC-ORD for non-metric multidimensional scaling, or NMDS. I wanted to learn how to do it in R for my stream ecology research. The candy gave us a dataset we could collect, analyze, and talk through in one lab meeting.

Start with distance

Each sample had seven color counts. How could we compare two samples across all seven colors at once? We used a distance measure called Bray–Curtis. It gives us one number for each pair of samples. A small distance means their color counts were similar. A large distance means they differed more.

Even with four samples, there are six pairs to compare. Our 13 samples gave us 78 pairs. That’s a lot of numbers to make sense of in a table.

Turn those distances into a picture

NMDS gives each sample a point on a plot. It tries to place more similar samples close together and more different samples farther apart. The “non-metric” part means it works with the order of the distances. If sample A is more similar to sample B than to sample C, NMDS tries to put A closer to B.

Each point represents one candy sample. M&M’s and Skittles occupy separate parts of the plot when compared using their raw color counts. Point shapes show packaging. NMDS stress = 0.051.

When I showed my labmates the plot, they could connect the groups they saw to the candy they had counted. I use the same idea in my stream research. Each field sample contains counts of different aquatic invertebrates, and NMDS helps me see which samples have similar communities and whether samples from different streams or habitats tend to group together.

We used raw color counts for this exercise. In a research project, the question you’re asking guides how you prepare the data and which distance measure you choose.

View the data and R code on GitHub


Leave a Reply

Your email address will not be published. Required fields are marked *