-
Notifications
You must be signed in to change notification settings - Fork 2.1k
How to get vertical lines in legend key using ggplot2 for geom_pointrange() type graphic #1389
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
Comments
You should probably redefine draw_key_pointrange. draw_key_pointrange <- function(data, params, size) {
grobTree(
segmentsGrob(0.5, 0.1, 0.5, 0.9,
gp = gpar(
col = alpha(data$colour, data$alpha),
lwd = data$size * .pt,
lty = data$linetype,
lineend = "butt"
),
arrow = params$arrow
),
draw_key_point(transform(data, size = data$size * 4), params)
)
} |
Minimal reprex: df <- data.frame(x = 1:3, y = 1:3)
ggplot(df, aes(x, y, colour = factor(x))) +
geom_pointrange(aes(ymin = y - 1, ymax = y + 1)) |
Fixed in 1ee49e2 |
How could the opposite be achieved (horizontal line ranges in legends), if we used horizontal Minimal reprex (showing vertical lines in legends):
|
Try ggstance? |
require(ggstance)
require(ggplot2)
df <- data.frame(x = 1:3, y = 1:3)
ggplot(df, aes(x, y, colour = factor(x))) +
geom_pointrangeh(aes(xmin = x - 1, xmax = x + 1)) yes this works ( luckily ggstance has the geom_pointrangeh version ! ) |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
As outlined on SO:
For ggplot2 graphics that have a symbol for a point estimate and a vertical line representing a range about that estimate (95% confidence interval, Inter-quartile Range, Minimum and Maximum, etc) I cannot get the legend key to show the symbol with a vertical line. Since
geom_pointrange()
only has arguments forymin
andymax
, I would think the intended (default) functionality ofgeom_pointrange(show_guide=T)
would be to have vertical lines (I say default because I understand that withcoord_flip
one could make horizontal lines in the plot). I also understand that having vertical lines in the legend key when the legend position is right or left will have the vertical lines "run together"...but for legends in the top or bottom having a vertical line through the symbol means that the key will match what appears in the plot.Yet the approaches I've tried still put horizontal lines in the legend key:
Default
show_guide=T
forgeom_pointrange
yields desired plot but has horizontal lines in legend key where vertical is desired (so as to match the plot):An attempt with
geom_point
andgeom_segment
together yields desired plot but has horizontal lines in legend key where vertical is desired (so as to match the plot):An attempt with
geom_point
andgeom_vline
together yields desired legend key but does not respect theymin
andymax
values in the plot:How do I get the legend key of the 3rd graph but the plot of one of the first two?
Attempted answers on (as outlined on SO):
1. Using geom_point(show_guide=T) + geom_segment(show_guide=F) + geom_vline(show_guide=T) where vline is plotted out of range of data and then coord_cartesian() excludes the vline.
My solution involves plotting a vertical line with
geom_vline(show_guide=T)
for an x-value that is out of the bounds of the displayed x-axis along with plottinggeom_segment(show_guide=F)
:2. Using grid and gtable:
The text was updated successfully, but these errors were encountered: