Skip to content

Redundant legend entries in facet_*() #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
talgalili opened this issue Dec 29, 2015 · 4 comments
Closed

Redundant legend entries in facet_*() #378

talgalili opened this issue Dec 29, 2015 · 4 comments

Comments

@talgalili
Copy link
Contributor

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)
@cpsievert
Copy link
Collaborator

A more minimal example of the same problem:

library(plotly)
g <- ggplot(mtcars, aes(x = mpg, y = disp, color = factor(cyl))) + geom_point() + facet_wrap(~vs)
ggplotly(g)

@cpsievert cpsievert changed the title Make scatterplot matrix (with facets) while having colors for subgroups Redundant legend entries in facet_*() Dec 29, 2015
@cpsievert
Copy link
Collaborator

This should be fixed in f1900cc. Let us know if you still have problems.

@talgalili
Copy link
Contributor Author

In the minimal example the issue is resolved, but in the example I originally gave the issue still remains:

image

@beaulucas
Copy link

beaulucas commented Dec 7, 2017

I have debugged this issue a bit. It appears when you specify a scale_color|fill_ value without declaring the relevant color|fill|etc in any aesthetics, the duplicate legends per facet appear.

These work:

cars_plot <- ggplot(mtcars, aes(x = mpg, y = disp, color = factor(cyl))) +
  geom_point() + 
  facet_wrap(~vs)
cars_plot
ggplotly(cars_plot)
cars_plot <- ggplot(mtcars, aes(x = mpg, y = disp, color = factor(cyl), fill = factor(cyl))) +
  geom_point() +
  facet_wrap(~vs)
cars_plot
ggplotly(cars_plot)

Both produce this:

screen shot 2017-12-07 at 1 27 08 pm

This doesn't work:

cars_plot <- ggplot(mtcars, aes(x = mpg, y = disp, color = factor(cyl), fill = factor(cyl))) +
  geom_point() + 
  scale_fill_brewer() +
  facet_wrap(~vs)
cars_plot
ggplotly(cars_plot)

Code above takes out the fill in the aes mapping now, but keep the scale_fill_brewer() call. This is useless code as there is no fill aesthetic to work on, but it breaks the plotly legend.

screen shot 2017-12-07 at 1 26 08 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants