Reputation: 879
I am having a 'classical' ggplot problem, very similar to the question discussed here. I tried to solve it like suggested there, but somehow it does not work.
The dataset is:
overall <- structure(list(organization = c("orga_arts", "orga_arts", "orga_arts",
"orga_arts", "orga_environment", "orga_environment", "orga_environment",
"orga_environment", "orga_humanrights", "orga_humanrights", "orga_humanrights",
"orga_humanrights", "orga_labour", "orga_labour", "orga_labour",
"orga_labour", "orga_neighbor", "orga_neighbor", "orga_neighbor",
"orga_neighbor", "orga_none", "orga_none", "orga_none", "orga_none",
"orga_others", "orga_others", "orga_others", "orga_others", "orga_party",
"orga_party", "orga_party", "orga_party", "orga_patriotic", "orga_patriotic",
"orga_patriotic", "orga_patriotic", "orga_religious", "orga_religious",
"orga_religious", "orga_religious", "orga_sports", "orga_sports",
"orga_sports", "orga_sports", "orga_welfare", "orga_welfare",
"orga_welfare", "orga_welfare"), among_participants = c(8.5,
8.5, 8.5, 8.5, 8.7, 8.7, 8.7, 8.7, 2.6, 2.6, 2.6, 2.6, 11.4,
11.4, 11.4, 11.4, 5.4, 5.4, 5.4, 5.4, 49.9, 49.9, 49.9, 49.9,
4.9, 4.9, 4.9, 4.9, 5.6, 5.6, 5.6, 5.6, 1, 1, 1, 1, 11.8, 11.8,
11.8, 11.8, 19.1, 19.1, 19.1, 19.1, 4.4, 4.4, 4.4, 4.4), value = c(10.7,
10.6, 11.9, 11, 12.9, 10.9, 12.8, 8.8, 4, 5.5, 5.4, 3.2, 12.3,
10.5, 12, 14.3, 6.9, 7.8, 7.6, 6.5, 46.9, 45.3, 45.1, 47.7, 5.4,
4.3, 5.4, 5.1, 6.5, 9, 4.5, 5.8, 0.9, 1.2, 1.2, 1.7, 13.2, 12.5,
12, 10.5, 20.8, 17.3, 19.2, 21.5, 5.7, 5.4, 4.5, 5.4), type = c("issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_corona", "issue_corona_eco", "issue_racism", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_corona_eco",
"issue_racism", "issue_climate", "issue_corona", "issue_racism",
"issue_climate", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_racism", "issue_corona", "issue_corona_eco", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona", "issue_climate",
"issue_racism", "issue_corona_eco", "issue_corona")), row.names = c(NA,
-48L), class = "data.frame")
I would like to do something like:
ggplot(overall) +
geom_segment(aes(x = among_participants, xend = value,
y=organization, yend=organization, color=type),
arrow = arrow(length = unit(0.5, "cm")),
position=position_dodge(width=space_between_bars))
but obviously with parallel arrows... I know I could do geom_linerange
, but then I miss the arrow heads that are important to show the direction of change with the design of the figure I had in mind.
I tried doing something like:
overall$organization_dodged <- ave(as.numeric(overall$organization), overall$organization,
FUN = function(x) x + rnorm(length(x), sd = .1))
and then doing the geom_segment
with scale_y_continuous
per the suggested answer in the question linked above but I could not get it to work.
I also saw in this thread that there was supposed to be an update on this 10 years ago. Maybe that happened, and I am missing an easy fix here that I can't see now?
Upvotes: 0
Views: 225
Reputation: 66490
I bet this can be done more elegantly, but this seems to work:
space_between_bars <- 0.2
overall$type_y_adj = scale(as.numeric(as.factor(overall$type))) * -space_between_bars
overall$y = as.numeric(as.factor(overall$organization)) + overall$type_y_adj
ggplot(overall) +
geom_segment(aes(x = among_participants, xend = value, y=y, yend=y, color=type),
arrow = arrow(length = unit(0.1, "cm"))) +
scale_y_continuous(breaks = 1:length(unique(overall$organization)),
labels = unique(overall$organization), minor_breaks = NULL)
Upvotes: 2