1

I have objects moving through different places over time, the plots look like this (but with many more paths):

ggplot(data = df, aes(
  y = place,
  x = value,
  color = order,
  group = order
)) +
  geom_step(alpha = 0.5) +
  theme(legend.position = "bottom") +
  guides(color = guide_legend(ncol = 1)) +
  geom_point(alpha = 0.5) +
  facet_wrap( ~ order)

enter image description here

I'd like to combine the facets into one plot:

ggplot(data = df, aes(
  y = place,
  x = value,
  color = order,
  group = order
)) +
  geom_step(alpha = 0.5) +
  theme(legend.position = "bottom") +
  guides(color = guide_legend(ncol = 1)) +
  geom_point(alpha = 0.5)

enter image description here

The problem I have with this is the overlapping. I would like to nudge/move every different color of geom_step() up by a few pixels (maybe a linewidth), so that overlapping lines appear thicker. I have tried this R - ggplot dodging geom_lines but changing the x- and y-coordinate messes up the plot.

ggplot(data = df, aes(
  y = value,
  x = place,
  color = order,
  group = order
)) +
  geom_step(alpha = 0.5, direction = "vh", position = position_dodge(width = 0.5)) +
  theme(legend.position = "bottom") +
  guides(color = guide_legend(ncol = 1)) +
  coord_flip()

enter image description here

I hope I was clear about my desired output. I'm grateful for any hints!

the data:

df <- structure(list(place = structure(c(1L, 7L, 8L, 2L, 8L, 4L, 8L, 
11L, 9L, 10L, 9L, 7L, 6L, 7L, 1L, 7L, 8L, 3L, 8L, 5L, 9L, 11L, 
9L, 10L, 8L, 7L, 6L, 7L), .Label = c("A", "B", "C", "D", "E", 
"F", "G", "H", "I", "J", "K"), class = "factor"), order = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", 
"b"), class = "factor"), value = c(0, 38.0069999694824, 254.986999988556, 
266.786999940872, 358.447000026703, 368.375, 613.148000001907, 
626.457999944687, 778.240999937057, 790.655999898911, 844.833999872208, 
914.274999856949, 925.282999992371, 952.84299993515, 0, 38.3450000286102, 
80.5469999313354, 93.7960000038147, 188.280999898911, 199.918999910355, 
380.635999917984, 385.131000041962, 447.441999912262, 455.503999948502, 
528.233000040054, 677.162999868393, 690.805000066757, 713.063999891281
)), row.names = c(NA, -28L), class = "data.frame")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
f.lechleitner
  • 3,554
  • 1
  • 17
  • 35
  • Try switching to `position_dodge2` and adjusting width there—is that closer to what you want? – camille Mar 12 '19 at 13:37
  • Unfortunately no. The dodging is working when i change my x and y and flip the axes, but this messes up the plot because the place is on the x-axis, so the points are plotted in ascending order according to the x-axis. This leads then to the line going from right to left (back in time) in the plot with the flipped coordinates. – f.lechleitner Mar 12 '19 at 14:55

1 Answers1

1

Okay, so after some more googling i stumbled upon the ggstance-package, which includes a vertical version of position_dodge which does exactly what i need:

library(ggstance)

    ggplot(data = df, aes(
      y = place,
      x = value,
      color = order,
      group = order
    )) +
      geom_step(position = position_dodge2v(height = 0.2, preserve = "single")) +
      theme(legend.position = "bottom") +
      guides(color = guide_legend(ncol = 1))

enter image description here

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35