Skip to content

Label data with values from two columns #34

Closed

Description

@mirkov

Hello,

I would like to reproduce the figure from Wikipedia's page on the bionmial probability: image link

I would assume that my data should have 4 columns:

  • k (x-axis)
  • probability (y-axis)
  • p (holding values 0.5 and 0.7)
  • n (holidng values 20 and 40)

How to specify p and n for color (and thereby legend I assume)? Right now I have (approximately)

                               (geom:point :k :probability :color :p)

Is this the right place to ask these questions. Or is reddit's Common Lisp channel better?

Activity

  1. snunez1 commented on Sep 23, 2026

    @snunez1
    Contributor

    I think that's close. I suggest using the gamma plot as a framework and then overlaying the ggplot style stuff:

    https://lisp-stat.dev/docs/manuals/distributions/ (bottom of page)

    Once that works you should be able to replace the mark points with a geom points spec. Also see:

    https://lisp-stat.dev/docs/cookbooks/quick-plot/#function-curves

  2. mirkov commented on Sep 25, 2026

    @mirkov
    Author

    Thank you for pointing me to function curves. They escaped my initial perusal.

    But I don't think that answers my question, which is:

    • how to use two columns to identify a data set.

    In my case, the binomial distribution function is a value of k with both p and n as parameters. The plot image link contains three curves, for three combinations of p and n: 0.5 & 20, 0.7 & 20, and 0.5 & 40.

    In my dataframe I have two columns, one for p, one for n. I can combine them as a string, into a p&k column and use it for the :color parameter of geom:point Or is there a built-in mechanism to do that?

  3. snunez1 commented on Sep 27, 2026

    @snunez1
    Contributor

    I took the path of trying this with ggplot, and IMO the most practical solution, both there and in lisp-stat is the one that you came up with: add a column with the color mapping. The important parts to remember here are:

    • Keywords vs. strings for :color. A keyword like :color :group maps a data column to color and gives you a legend. A string like :color "teal" paints every point one color.
    • One field per channel. :color takes a single field, just like aes(colour = ...) in ggplot2. So the answer to the issue is the same as in R: make one column that combines p and n, and map :color to it.

    You could probably do this with layers too, but I tend to take the shortest route possible with plots, and that's going to be a column with color encoding.

  4. mirkov commented on Sep 27, 2026

    @mirkov
    Author

    I will try to implement that.

    I thought about it more on my end, and realized I need to do a bit more thinking.

    Based on my recollection of Wilkinson's Grammar of Graphics (I need to dig out that book),

    Legend is just another axis, in addition to x & y.

    If that is so, it does not make sense to combine to variables (n & p) as another column, just as it would not make sense to combine to variables into x and then plot y vs that new x.

    After implementing the straightforward approach, I will go to Wilkinson's book and look through it.

  5. mirkov commented on Sep 30, 2026

    @mirkov
    Author

    I looked at Leland Wilkinson's Grammar of Graphics, and in Chapter 12 the second plot includes two legends. In retrospect it is obvious how to do it with my problem. Use two attributes to render a point

    • Use point shape (circle, square, ...) for one dimension (p)
    • Use point color for the other dimension (n).

    And that is what I did. I defined geom:point rendering baswed on color and shape (I guessed that :shape is a valid keyword, which it is):

    (defun plot-binomial-pdf ()
      (let ((df (df:make-df '(k-vec pmf-vec N-vec p-vec)
                            `(,(april "k_vec") ,(april "pmf_vec") ,(april "N_vec") ,(april "p_vec")))))
        (plot:plot (vega:make-plot df
                                   '(:title "Binomial distribution probability mass function")
                                   (geom:point :k-vec :pmf-vec :color :p-vec :size :N-vec)
                                   (gg:theme :width 360 :height 220 )))))
    

    Works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions