-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
dumbbell three X arguments #59
Comments
UPDATE: I'm using IF statements to decide which dumbbell graph is drawn first but this seems a subpar solution... |
Well met, @pedrocr83! This is a super interesting request (i.e. making this a more generic function). Lemme ponder a bit what said API would look like. In the interim, here's an example for how to build this with with three points (it works for library(hrbrthemes)
library(tidyverse)
tibble(
val1 = c(3, 2, 4),
val2 = c(1, 4, 5),
val3 = c(5, 8, 6),
cat = factor(month.name[1:3], levels = rev(month.name[1:3]))
) -> xdf
ggplot() +
# this draws leading eye-tracker dotted line from the category name
# to the first point (helps readers follow the values to the categories)
geom_segment(
data = gather(xdf, measure, val, -cat) %>%
group_by(cat) %>%
top_n(-1) %>%
slice(1) %>%
ungroup(),
aes(x = 0, xend = val, y = cat, yend = cat),
linetype = "dotted", size = 0.5, color = "gray80"
) +
# this draws the underlying full segment intelligently choosing
# the correct min/max points
geom_segment(
data = gather(xdf, measure, val, -cat) %>%
group_by(cat) %>%
summarise(start = range(val)[1], end = range(val)[2]) %>%
ungroup(),
aes(x = start, xend = end, y = cat, yend = cat),
color = "gray80", size = 2
) +
# this draws the
geom_point(
data = gather(xdf, measure, value, -cat),
aes(value, cat, color = measure),
size = 4
) +
# i just extended the scale a bit + put axis on top; choose aesthetics that work
# for you
scale_x_comma(position = "top", limits = c(0, 10)) +
scale_color_ipsum(name = "A real legend title") +
labs(
x = "Description of the value", y = NULL,
title = "A good plot title"
) +
theme_ipsum_rc(grid = "X") +
theme(legend.position = "top") Do not hesitate to ask any and all q's abt ^^. Very glad to lend some assistance. |
@hrbrmstr awe...some! Thanks for the effort! |
Hello @hrbrmstr, thanks for your code! I am trying to use the same idea where I have 3 values on 1 dumbbell. However, when I tried to run your exact code, I run into error. Below I copy the error message generated by R, and would greatly appreciate your help in pointing out where I could go and fix:
After running rlang::last_error() :
My R version (if it is relevant)
Much appreciated for your help in advance! Elly |
@ellyyuyang without the tibble its kinda of hard to debug but i'll give my example after that awesome piece of code @hrbrmstr gave me: d <- tibble(normal = value1 , notnormal = value2 , sample = value3 , metric = factor(stringvalue)) getComparisonPlot<-function(d) |
Hello @pedrocr83 , appreciate your help! It may be due to something iffy happened to my R studio. My issue has been resolved after restarting R studio. Cheers! @hrbrmstr thanks for your code again! |
@pedrocr83 I was trying to play around with the code and explore the possibly of reordering the dumbbells according to one of the variables but failed, would like to see if you would have any insights to it! Much appreciated! So I tried to reorder the order of months (in @hrbrmstr 's original code) by the val3. so I added the following code before ggplot
But this does not perform the reordering of the dumbbells. Do you have any thoughts on this? Best, |
Sorry @ellyyuyang are you still having this issue? somehow i missed the notification |
I'm using geom_dumbbell (really cool btw), to plot 3 values on the x-axis to compare the distances between them. Because it only takes x and x_end args i'm using geom_dumbbell twice, efectively solving my problem. However, since my data changes everytime i run the plot sometimes the line connecting x and x_end of the last plot being created goes over one of the points. Can you see a way around this or another way of plotting 3 values using geom_dumbbell. (example attached)
The text was updated successfully, but these errors were encountered: