Skip to content

change in names(object$facet$params$cols) #2660

Closed

Description

@rmheiberger

Brief description of the problem

There are changes in ggplot2_2.2.1.9000 that make the
microplot/demo/latex-ggplot.r file generate an error.

library(microplot)
demo("latex-ggplot"              , package="microplot", ask=TRUE)

The last item in the demo

latex(ggv, height.panel=.41,
height.x.axis=.2, ## inch
width.y.axis=.3, ## inch
rowlabel="group",
vectorgraph.colname="Graph Panels")

does not complete. I traced it to my function
microplot.ggplot in file R/microplot.ggplot.R, lines 78 and 79

The underlying issue is that ggplot2_2.2.1 reports
Browse[1]> names(object$facet$params$cols)
NULL

and ggplot2_2.2.1.9000 reports
Browse[1]> names(object$facet$params$cols)
character(0)

I could just change my line 79 to look for either character(0) or NULL.
However, since there is a change in behavior in the results you are reporting, I will ask your advice
before making a change.

Rich

Activity

  1. clauswilke commented on May 26, 2018

    @clauswilke
    Member

    Thanks for your report. Would you mind providing a minimal reprex using only ggplot2 code? We don't know what's happening in lines 78 and 79 of your code.

  2. rmheiberger commented on May 26, 2018

    @rmheiberger
    Author
  3. hadley commented on May 28, 2018

    @hadley
    Member

    Recreating @rmheiberger's email response with markdown to improve readability:

    > packageVersion("ggplot2")
    [1] ‘2.2.1’
    > class(dd)
    [1] "data.frame"
    > class(ggv)
    [1] "gg"     "ggplot"
    > ggv$facet$param$cols
    NULL
    
    > packageVersion("ggplot2")
    [1] ‘2.2.1.9000’
    > names(ggv$facet$params$cols)
    character(0)
    

    This is the relevant ggplot code from the demo

    ggv <- ggplot(dd, aes(x, y)) +
      scale_x_continuous(limits = c(-120, 120)) +
      scale_y_continuous(limits = c( -20, 220)) +
      theme(axis.text=element_text(size=6)) +
      geom_point(shape=dd$ggg, col=dd$col, size=5) +
      facet_grid(rr.cc ~ .)
    ggv
  4. hadley commented on May 28, 2018

    @hadley
    Member

    Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.

  5. added
    reprex
    on May 28, 2018
  6. rmheiberger commented on May 28, 2018

    @rmheiberger
    Author

    Totally simplified

    DF <- data.frame(rr=letters[c(1,1,2,2)],
                     cc=LETTERS[c(3,4,3,4)],
                     x=rnorm(4),
                     y=rnorm(4))
    
    DF$rr.cc <- with(DF, interaction(rr,cc))
    
    ggv <- ggplot(DF, aes(x, y)) +
      geom_point(shape=DF$rr.cc) +
      facet_grid(rr.cc ~ .)
    ggv
    
    names(ggv$facet$params$cols)
    
    > names(ggv$facet$params$cols)
    character(0)
    > packageVersion("ggplot2")
    [1] ‘2.2.1.9000’
    >
    
    > names(ggv$facet$params$cols)
    NULL
    > packageVersion("ggplot2")
    [1] ‘2.2.1’
    

    In my code, if there is no name for the column of panels in the graphics window, then I assign one.
    The change in value for

     names(ggv$facet$params$cols)
    

    breaks my test for the existence of a name. Obviously I can test for the new value.
    The issue is whether this change is intentional or an accidental byproduct.
    Please let me know whether I should change my code.

  7. clauswilke commented on May 28, 2018

    @clauswilke
    Member

    When rows or columns are not specified in facet_grid() their names are now generated with quos(), which generates an empty named list:

    ggplot2/R/facet-grid-.r

    Lines 189 to 190 in 5e9b688

    if (is.null(cols)) {
    cols <- quos()

    rlang::quos()
    #> named list()
    names(rlang::quos())
    #> character(0)

    So going forward, you should expect the result of names(ggv$facet$params$cols) to be character(0).

    More importantly, though, geom_point(shape=DF$rr.cc) is not valid ggplot2 code and things will break for you at some point if you use this construct, in particular if you're using it in faceted plots. You need to write geom_point(aes(shape=rr.cc)).

  8. rmheiberger commented on Jun 3, 2018

    @rmheiberger
    Author

    Thank you. I have changed the code to check on character(0).

    Your suggestion about geom_point does not agree with ?aes_linetype_size_shape which includes examples without aes(), for example geom_point(shape=5).

    DF <- data.frame(rr=c(49:57, 65:67),
                     x=1:12,
                     y=1:12)
    
    ggsh <- ggplot(DF, aes(x, y)) +
      geom_point(shape=DF$rr) +
      facet_grid(rr ~ .)
    ggsh
    
    ggaes <- ggplot(DF, aes(x, y)) +
      geom_point(aes(shape=DF$rr)) +
      facet_grid(rr ~ .)
    ggaes
    

    ggsh does what I want. ggaes gives an error.

    > ggaes
    Error: A continuous variable can not be mapped to shape
    
  9. clauswilke commented on Jun 3, 2018

    @clauswilke
    Member

    @rmheiberger Your code is simply not valid ggplot2 and is not guaranteed to work. See example below.

    @hadley I feel this needs to be documented better. I see people making this mistake all the time, and usually all I can say is "don't do this" but I can't point to an article explaining why this is a problem. Can you think of a good place where this could go? Maybe a ggplot2 vignette? Or a blogpost somewhere?

    Example for how things go wrong when you hand a data column to aes() or to a parameter:

    library(ggplot2) 
    
    df <- data.frame(x = 1:6,
                     y = 1:6,
                     label = c("a", "b", "c", "a", "b", "c"),
                     colour =  c("magenta", "green", "cyan", "magenta", "green", "cyan"))
    
    # doesn't work, the indicated colors are not used, and colors also don't match within facets
    ggplot(df, aes(x, y, colour = df$colour)) +
      geom_point(size = 3) +
      facet_wrap(~label)

    # this works in the sense that the indicated colors are used, but colors still don't match
    # within facets
    ggplot(df, aes(x, y, colour = df$colour)) +
      geom_point(size = 3) +
      facet_wrap(~label) +
      scale_colour_identity()

    # directly handing the colors to `geom_point()` also doesn't work
    ggplot(df, aes(x, y)) +
      geom_point(size = 3, colour = df$colour) +
      facet_wrap(~label)

    # finally, if we map the colors via aes things work as expected
    ggplot(df, aes(x, y, colour = colour)) +
      geom_point(size = 3) +
      facet_wrap(~label) +
      scale_colour_identity()

    Created on 2018-06-03 by the reprex package (v0.2.0).

  10. rmheiberger commented on Jun 4, 2018

    @rmheiberger
    Author

    Thank you for writing back.

    This example doesn't show how to produce in valid ggplot2 code what I have successfully
    produced in both ggplot2_2.2.1.9000 and ggplot2_2.2.1. I specifically want the 12 characters
    c("1","2","3","4","5","6","7","8","9","A","B","C") as my plotting characters.

    ggsh in my previous posting does exactly what I want. Please show me how to do that in valid ggplot2 syntax.

  11. clauswilke commented on Jun 4, 2018

    @clauswilke
    Member

    @rmheiberger I recommend you read up on how scales work, and then specifically scale_shape_identity() and scale_shape_manual(). If you have further questions, please direct them to https://community.rstudio.com or https://stackoverflow.com/. The issue system is meant for bugs or feature requests.

  12. rmheiberger commented on Jun 4, 2018

    @rmheiberger
    Author

    perfect. thank you.

    this does what I want.

    ggplot(DF, aes(x, y)) +
      geom_point(aes(shape=factor(c("1","2","3","4","5","6","7","8","9","A","B","C")))) +
      scale_shape_identity() +
      facet_grid(rr ~ .)
    

    As far as I am concerned you may close this issue.

    Rich

  13. lock commented on Dec 1, 2018

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  14. locked and limited conversation to collaborators on Dec 1, 2018
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

    reprex

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions