Skip to content

size in geom_point using variable with all but 1 as NAs #985

Description

@iagomosqueira

When passing a variable in the data.frame to the size argument in geom_point, if the column to be all but one as NA, all values get plotted equally, i.e. NAs are not dropped. If two non-NA values exist, the plot looks as expected.

Wanted to know if it is an unavoidable consequence of how size is calculated, or really a bug. It the later I could try finding/fixing it.

  df <- data.frame(year=rep(10:14, each=3), age=1:3, data=runif(15))

  # points sized as expected
  ggplot(df, aes(year, age)) + geom_point(aes(size=data))

  # convert all values but 2 into NAs
  df$data[1:13] <- NA
  #2 points sized and shown
  ggplot(df, aes(year, age)) + geom_point(aes(size=data), na.rm=TRUE)

  # convert all but one value into NA
  df$data[1:14] <- NA

  # plot shows equal-sized dots for all datapoints
  ggplot(df, aes(year, age)) + geom_point(aes(size=data), na.rm=TRUE)

I tried with different names for the data column, but it made no difference. Tested in both ggplot2 1.0.0 from CRAN and 1.0.0.99 from github.

Thanks

Activity

  1. hadley commented on Jun 12, 2015

    @hadley
    Member

    That is a strange one. I'll take a look. Even more minimal reprex:

    df <- data.frame(x = rep(1:2, each = 2), y = rep(1:2, 2), z = c(1, NA, NA, NA))
    ggplot(df, aes(x, y, size = z)) + geom_point()
  2. hadley commented on Jul 23, 2015

    @hadley
    Member

    Ah, this is a bug in scales::rescale():

    function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) {
      if (zero_range(from) || zero_range(to)) return(rep(mean(to), length(x)))
    
      (x - from[1]) / diff(from) * diff(to) + to[1]
    }

    If from has zero_range(), it needs to preserve missing values in x.

  3. hadley commented on Jul 23, 2015

    @hadley
    Member
  4. locked as resolved and limited conversation to collaborators on Jun 19, 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions