Skip to content

Redundant legend entries in facet_*() #378

@talgalili

Description

@talgalili

Currently, using facets for scatterplot matrix, combined with subgroup colors, produces a list of groups labels for each facet, making the graph un readable. Here is how it looks:

image

Instead of looking like this (in ggplot2):

image

Here is the code to reproduce:

  # https://gastonsanchez.wordpress.com/2012/08/27/scatterplot-matrices-with-ggplot/
  # another option
  makePairs <- function(data) 
  {
    grid <- expand.grid(x = 1:ncol(data), y = 1:ncol(data))
    grid <- subset(grid, x != y)
    all <- do.call("rbind", lapply(1:nrow(grid), function(i) {
      xcol <- grid[i, "x"]
      ycol <- grid[i, "y"]
      data.frame(xvar = names(data)[ycol], yvar = names(data)[xcol], 
                 x = data[, xcol], y = data[, ycol], data)
    }))
    all$xvar <- factor(all$xvar, levels = names(data))
    all$yvar <- factor(all$yvar, levels = names(data))
    densities <- do.call("rbind", lapply(1:ncol(data), function(i) {
      data.frame(xvar = names(data)[i], yvar = names(data)[i], x = data[, i])
    }))
    list(all=all, densities=densities)
  }

  # expand iris data frame for pairs plot
  gg1 = makePairs(iris[,-5])

  # new data frame mega iris
  mega_iris = data.frame(gg1$all, Species=rep(iris$Species, length=nrow(gg1$all)))

  library(ggplot2)
  # pairs plot
  p <- ggplot(mega_iris, aes_string(x = "x", y = "y")) + 
    facet_grid(xvar ~ yvar, scales = "free") + 
    geom_point(aes(colour=Species), na.rm = TRUE, alpha=0.8) + # geom_smooth(method = lm) + 
    stat_density(aes(x = x, y = ..scaled.. * diff(range(x)) + min(x)), 
                 data = gg1$densities, position = "identity", 
                 colour = "grey20", geom = "line")

  library(plotly)
  ggplotly(p)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions