Comments (8)
I imagine this would get hairy. If we discard oob breaks before labelling, labels given as atomic vectors will become out of sync. In addition, minor breaks might be miscalculated without oob breaks. I'll keep this issue open as a prompt to explore this more fully, but the answer for now is 'probably not'.
from ggplot2.
This is because the labelling function is applied to breaks before the out-of-bounds breaks are censored.
In your second example, you'd need to discard the out-of-bound breaks and it works as intended.
library(palmerpenguins)
library(ggplot2)
library(scales)
hold_3rd <- function(x) {
x[-3] <- ""
x
}
penguins |>
ggplot() +
geom_point(
aes(x = bill_length_mm,
y = body_mass_g),
) +
scale_x_continuous(
labels = \(x) hold_3rd(comma(x)),
breaks = \(x) oob_discard(extended_breaks()(x), x)
)
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).
Created on 2024-05-05 with reprex v2.1.0
from ggplot2.
Thanks @teunbrand. It'd be great to be able to do this with oob's other than oob_discard. Could the labelling function be applied to breaks after the out-of-bounds breaks are censored?
from ggplot2.
It would be useful, but don't want to break everything! Feel free to close whenever
from ggplot2.
The main use-case for this would be for a labelling function that labels every second break, and leaves every other one as "". It would work much more intuitively, if it always started from the first break within bounds
from ggplot2.
I have thought about this some more, and while we could implement this in ggplot2 without problems for ggplot2, this might unecessarily break other people's packages. Back during reverse dependency checks for 3.5.0, I came across a bunch of code in packages that made unorthodox* use of label functions that would break again if this were to be changed. For that reason, I don't want to change the way this works.
* = using lookup tables or returning fixed-length atomic vectors
Now, for your use case, I had forgotten that breaks arrive at the labelling function in a pre-censored state (i.e. oob breaks are NA
). You can exploit this as follows. Similar to the reprex:
library(ggplot2)
only_show_nth <- function(n) {
force(n)
function(x) {
i <- which(is.finite(x))
x[-i[n]] <- ""
x
}
}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(
labels = only_show_nth(2)
)
Similar to the use-case you describe:
show_every_nth <- function(n) {
force(n)
function(x) {
i <- which(is.finite(x))
i <- i[seq_along(i) %% n == 0]
x[-i] <- ""
x
}
}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(
labels = show_every_nth(2)
)
Created on 2024-05-08 with reprex v2.1.0
from ggplot2.
That's awesome, thanks @teunbrand.
Works as expected for positional scales, but not for colour scales?
Also, I assume you're not interested in putting an argument in the scales::label_*
functions to support this?
library(tidyverse)
show_every_nth <- function(n = 2, offset = 0) {
force(n)
function(x) {
i <- which(is.finite(x))
i <- i[seq_along(i) %% n == (offset + 1)]
x[-i] <- ""
x
}
}
ggplot(mpg, aes(displ, hwy, colour = displ)) +
geom_point() +
scale_x_continuous(labels = show_every_nth(2)) +
scale_y_continuous(labels = show_every_nth(2)) +
scale_colour_gradientn(colors = viridis::viridis(9), labels = show_every_nth())
ggplot(mpg, aes(displ, hwy, colour = hwy)) +
geom_point() +
scale_x_continuous(labels = show_every_nth(2)) +
scale_y_continuous(labels = show_every_nth(2)) +
scale_colour_gradientn(colors = viridis::viridis(9), labels = show_every_nth())
Created on 2024-05-09 with reprex v2.1.0
from ggplot2.
Instead of an argument in a scales::label_* function, it might work better as a function.
Let me know if you'd like to implement something like this in {scales}. Otherwise, I'll chuck it in {ggblanket}
label_every_nth <- function(n = 2, offset = 0, ...) {
function(x) {
i <- which(is.finite(x) | is.character(x) | is.factor(x) | is.logical(x))
i <- i[seq_along(i) %% n == (offset + 1)]
if (is.numeric(x)) x <- scales::comma(x, ...)
else x <- format(x, ...)
x[-i] <- ""
x
}
}
from ggplot2.
Related Issues (20)
- Should the diamonds dataset factor variables be unorderd? HOT 3
- coord_radial() and coord_polar() are not connecting the start and end points anymore (v3.5.1) HOT 3
- Suggestion for Improved Error Message for `geom_area()` with Discrete X-Axis
- `geom_area()` provides unclear error message when discrete x-axis is used HOT 1
- labeller example that uses an optional argument
- `scale_*_continuous` fails to render breaks if axis is reversed HOT 4
- When x values contain 0, using coord_trans("sqrt") removes the axis line HOT 4
- {marquee} integration HOT 1
- Encapsulate or export `{wrap/grid}_as_facets_list()`.
- ggplotly does not work when adding scale_fill_binned HOT 1
- Superfluous guidance in "Creating a new theme" section of ggplot2 packages vignette? HOT 2
- ggplot2 ignores the user's input for transparency via NA's in scale_fill_manual() HOT 12
- `guide_colorsteps()` can't set the `position` HOT 1
- strip.placement = "outside" and strip.switch.pad.wrap don't work together perfectly for facet_wrap. HOT 2
- `orientation` for `geom_step()` HOT 1
- Internal function `find_x_overlaps()` has bug
- layer code with geom = "rug" no longer works as expected in Development version
- stat_density() error and bw.nrd() man file are in conflict on valid inputs HOT 2
- binned_scale() function missing after version 3.4.1? HOT 1
- scale_y_discrete() fails without data to plot
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 ggplot2.