Comments (4)
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.
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.
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)
- Release ggnewscale 0.4.7
- Multiple new_scale_fill doesn't work. HOT 9
- Support for plotly HOT 4
- Release ggnewscale 0.4.8
- Issue When Trying to Employ `guide_colourbar` ::::: v0.4.8 HOT 5
- Legend issues when using `guides(...)` HOT 4
- Add ability to name scales HOT 7
- ggnewscale CRAN note HOT 3
- Issues with next version of ggplot2 HOT 4
- Question on overriding scales on a pre-existing ggnewscale plot HOT 3
- Release ggnewscale 0.4.9
- Release ggnewscale 0.4.10
- new_scale_colour does not work with a scale_* function that includes a guide argument HOT 4
- Does not display two scales HOT 2
- Release ggnewscale 0.5.0
- Release ggnewscale 0.4.10 HOT 4
- new_scale_*() throws an error in ggplot2 3.5.0 HOT 4
- New Scales Clear Custom Attributes HOT 2
- setting legend name in `guides` vs. in `scale_manual` with `new_scale()` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ggnewscale.