Git Product home page Git Product logo

Comments (4)

eliocamp avatar eliocamp commented on August 11, 2024 1

Thank you for the last example. I'll take it into account for the future refactoring. I suspect is something that could be fixed by the new logic.

from ggnewscale.

eliocamp avatar eliocamp commented on August 11, 2024

The issues are fixed in the development version of ggnewscale (remotes::install_github(“eliocamp/ggnewscale”)).

I probably won't be sending it to CRAN soon because I think I rather publish a rewrite of the internals that fixes other bugs and makes the code much more easy to follow. But it requires a change in ggplot2 which is not available yet.

# Make up some values
library(ggplot2)
library(ggnewscale)

nn <- 6
n <- 3 * 5 * nn

m <- data.frame(
  id1 = factor(rep(8:10, each = n / 3)),  # No overlap to distinguish them more easily
  id2 = factor(rep(1:5, each = n / 5)),
  x = runif(n, 1, 80)
)

m[["y"]] <- 10 + m[["x"]] * runif(n, 1, 2)


ggplot(m, aes(x, y)) +
  geom_line(aes(color = id2), show.legend = FALSE) +
  geom_point(aes(color = id1), show.legend = TRUE) +
  scale_color_viridis_d()

Here, the line doesn’t contribute to the legend so the legend is only drawn with points instead of points and lines. The title of the legend inherits from the first layer. The legend is not drawn “for id2” nor id1, it’s drawn for the colour aesthetic, which combines all the unique values of id1 and id2.

The following examples work in the development version of ggnewscale

Default: shows both legends with lines

ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1)) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2)) +
  scale_color_viridis_d(name = "id2") 

Remove legend for first layer: shows only legend for second layer

ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1), show.legend = FALSE) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2)) +
  scale_color_viridis_d(name = "id2") 

Remove legend for second layer: shows only legend for first layer

ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1)) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2), show.legend = FALSE) +
  scale_color_viridis_d(name = "id2") 

Remove legends for both layers: shows no legend.

ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1), show.legend = FALSE) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2), show.legend = FALSE) +
  scale_color_viridis_d(name = "id2") 

Adds a smooth layer: shows legend only for that layer, with correct width.

ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1), show.legend = FALSE) +
  geom_smooth(aes(color = id1)) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2), show.legend = FALSE) +
  scale_color_viridis_d(name = "id2") 
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Created on 2023-11-28 with reprex v2.0.2

from ggnewscale.

dschlaep avatar dschlaep commented on August 11, 2024

This is great, thanks so much for the fix and response! I installed the dev-version and the examples work nicely.

In your example "Remove legend for first layer: shows only legend for second layer", if I drop in an (unnecessary but possible) show.legend = TRUE, then it reverts back to showing unexpected legends.

library(ggplot2)
library(ggnewscale)

nn <- 6
n <- 3 * 5 * nn

m <- data.frame(
  id1 = factor(rep(8:10, each = n / 3)),
  id2 = factor(rep(1:5, each = n / 5)),
  x = runif(n, 1, 80)
)

m[["y"]] <- 10 + m[["x"]] * runif(n, 1, 2)


# Remove legend for first layer: shows only legend for second layer
# this works
ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1), show.legend = FALSE) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2)) +
  scale_color_viridis_d(name = "id2")

# Remove legend for first layer: shows only legend for second layer
# this doesn't work
ggplot(m, aes(x, y)) +
  geom_line(aes(color = id1), show.legend = FALSE) +
  scale_color_viridis_d(name = "id1") +
  new_scale_color() +
  geom_line(aes(color = id2), show.legend = TRUE) +
  scale_color_viridis_d(name = "id2")

Created on 2023-11-28 with reprex v2.0.2

from ggnewscale.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.