Update: found an answer but I can't say I understand it:
https://stackoverflow.com/a/47498831/2192578
Original issue:
I have 2 plots that are exactly the same, with only 1 exception: the coloring (pictures below). How can I make it such that the paths are still connecting? I believe this happens because geom_path() is basically building 3 different plots and overlapping them. How can I color different traces of the same path instead?
For reference, this is the code I'm using basically:
demand %>% ggplot(aes(x = prices, y = quantities, color = active_segments)) +
geom_path()
vs
demand %>% ggplot(aes(x = prices, y = quantities) +
geom_path()
Sample data frame:
structure(list(prices = c(210, 211.5, 213, 214.5, 216, 217.5,
219, 220.5, 222, 223.5, 225, 226.5, 228, 229.5, 231, 232.5, 234,
235.5, 237, 238.5, 240, 241.5, 243, 244.5, 246, 247.5, 249, 250.5,
252, 253.5, 255, 256.5, 258, 259.5, 261, 262.5, 264, 265.5, 267,
268.5, 270, 271.5, 273, 274.5, 276, 277.5, 279, 280.5, 282, 283.5,
285, 286.5, 288, 289.5, 291, 292.5, 294, 295.5, 297, 298.5, 300,
301.5, 303, 304.5, 306, 307.5, 309), quantities = c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25), Segment1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Segment2 = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Segment3 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), active_segments = c("All segments",
"All segments", "All segments", "All segments", "All segments",
"All segments", "All segments", "All segments", "All segments",
"All segments", "All segments", "All segments", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3")), row.names = c(1L, 151L, 301L, 451L, 601L,
751L, 901L, 1051L, 1201L, 1351L, 1501L, 1651L, 1801L, 1951L,
2101L, 2251L, 2401L, 2551L, 2701L, 2851L, 3001L, 3151L, 3301L,
3451L, 3601L, 3751L, 3901L, 4051L, 4201L, 4351L, 4501L, 4651L,
4801L, 4951L, 5101L, 5251L, 5401L, 5551L, 5701L, 5851L, 6001L,
6151L, 6301L, 6451L, 6601L, 6751L, 6901L, 7051L, 7201L, 7351L,
7501L, 7651L, 7801L, 7951L, 8101L, 8251L, 8401L, 8551L, 8701L,
8851L, 9001L, 9151L, 9301L, 9451L, 9601L, 9751L, 9901L), class = "data.frame")