-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Feature request: Add position argument to geom_vline and geom_hline #4285
Comments
Use `after_scale` mapping evaluation.
https://ggplot2.tidyverse.org/reference/aes_eval.html
With it you can do everything `position_nudge` make possible and more.
…On Sat., Dec. 5, 2020, 12:11 a.m. Bill Denney, ***@***.***> wrote:
When working with a discrete scale, I'd like to be able to nudge a vline a
bit. But, geom_vline() doesn't use the position argument that I would
have expected. It would be helpful to add a position argument to
geom_vline() and geom_hline() to allow nudging the position.
In this example, I'd like to have the red vertical line go between the "1"
and "2" on the horizontal axis.
library(ggplot2)mydata <-
data.frame(
x=factor(c("1", "2")),
y=3:4
)
ggplot(mydata, aes(x=x, y=y)) +
geom_point() +
geom_vline(xintercept="2", colour="red")
<https://camo.githubusercontent.com/c46365086093bc2d4f07ebb111f5775396288680dc2780b5e986cb0a3edcf2fe/68747470733a2f2f692e696d6775722e636f6d2f314c556c6479662e706e67>
ggplot(mydata, aes(x=x, y=y)) +
geom_point() +
geom_vline(xintercept="2", colour="red", position=position_nudge(x=-0.5))#> Warning: Ignoring unknown parameters: position
<https://camo.githubusercontent.com/789ce832e35606459ea479bb24b6e539c140fa07060e64f70b46f4c1033fccce/68747470733a2f2f692e696d6775722e636f6d2f6d336b7339424e2e706e67>
Created on 2020-12-04 by the reprex package <https://reprex.tidyverse.org>
(v0.3.0)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4285>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAF6QNPNQNKWNRN4XYGYXUTSTFGABANCNFSM4UN6WQBQ>
.
|
I'm for adding
I think it doesn't work. Values mapped to a discrete scale is still discrete. library(ggplot2)
mydata <-
data.frame(
x=factor(c("1", "2")),
y=3:4
)
ggplot(mydata, aes(x, y)) +
geom_point() +
geom_vline(
aes(xintercept = stage(x, after_scale = xintercept - 0.5)),
data = data.frame(x = "2"),
colour = "red"
)
#> Error in xintercept - 0.5: non-numeric argument to binary operator |
I hadn't heard of With Is there a better way to use I agree with @yutannihilation, adding a library(ggplot2)
mydata <-
data.frame(
x=factor(c("1", "2")),
y=3:4
)
ggplot(mydata, aes(x, y)) +
geom_point() +
geom_vline(
aes(xintercept = after_scale(1.5)),
colour = "red"
)
#> Error: geom_vline requires the following missing aesthetics: xintercept
ggplot(mydata, aes(x, y)) +
geom_point() +
geom_vline(
aes(xintercept = stage(x, after_scale = 1.5)),
colour = "red"
) Created on 2020-12-06 by the reprex package (v0.3.0) |
the below works not sure what I am missing ? library(ggplot2)
mydata <-
data.frame(
x=factor(c("1", "2","3")),
y=3:5
)
ggplot(mydata, aes(x, y)) +
geom_point(size=3) +
geom_vline(
aes(xintercept = as.numeric(x) - 0.5),alpha=0.2,size=5,
colour = "red"
) Created on 2020-12-06 by the reprex package (v0.3.0) |
@smouksassi, I am surprised that works because it's providing a continuous value to a discrete scale. Thanks for pointing out that it does work. Your example does rely on |
If your x axis is virtually continuous, it might be easier to use numeric values and add the corresponding labels. |
The
At that point the type of scale does not matter, the categorisation between discrete scale and continuous scale is at a higher level of abstraction. Real (continuous) numbers get plotted. |
I don't think of the axis as "virtually continuous". It is a discrete axis where I want to separate the times that are before the discrete value of "Baseline" from the times at and after "Baseline". To me, it is discrete with two layers of discrete categories: The value shown on the x-axis and a grouping of values shown on the x-axis (before and at or after "Baseline"). The vline is to illustrate the second layer. |
this has always worked numeric on top of discrete ( as mentioned above at the end it is 1 ,2 ,3 that are plotted) but the reverse is not true when you supply discrete on numeric scale this will error out |
@smouksassi is right. It is okay and helpful to think of the axis. The discrete scale provides "macro organisation" for the groups e.g this boxplot/violin is centered at 1, that one is centered at 2 ..., then the boxes/violins are calculated in continuous form around those central values. |
Ah, now I understand better what you were both meaning, thank you. When you were saying that it was "virtually continuous" you were referring to the way that ggplot handles it and not the way that my data is logically considered. Thanks! Looping back to the original request, I do think that a |
I agree that a position argument could be helpful see below when faceting and with free_scales the above will not work. library(ggplot2)
mydata <-
data.frame(
x=factor(c("1", "2","3")),
y=c(3,4,5)
)
mydata$splits <- factor(mydata$x, labels=c("baseline","Cycle 1","Cycle 1"))
ggplot(mydata, aes(x, y)) +
geom_point(size=3,position=position_nudge(x=-0.1)) +
geom_vline(
aes(xintercept = as.numeric(x)+0.1) ,
alpha=0.2,size=5,
colour = "red"
)+
facet_grid(~splits,switch = "x", scales = "free_x", space = "free_x")+
theme(strip.placement = "outside") Created on 2020-12-07 by the reprex package (v0.3.0) |
When working with a discrete scale, I'd like to be able to nudge a vline a bit. But,
geom_vline()
doesn't use the position argument that I would have expected. It would be helpful to add aposition
argument togeom_vline()
andgeom_hline()
to allow nudging the position.In this example, I'd like to have the red vertical line go between the
"1"
and"2"
on the horizontal axis.Created on 2020-12-04 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: