Home > Mobile >  How to get rectangular paths using geom_link?
How to get rectangular paths using geom_link?

Time:09-16

I'm trying out geom_link for the first time to connect x and xend points in my data. I've followed the reference material for ggforce, but I can't seem to work out how to get my paths rectangular, just like a typical horizontal bar chart. Is there a way to do this? The code I have below returns a tapered off x.

library(tidyverse)
library(ggforce)

ex_data <- data.frame(
  id = letters[1:16],
  x = c(9.17, 11.81, 0.95, 5.61, 2.52, 1.78, 1.44, 2.40, 5.14, 8.25, 6.65, 4.44,
        2.71, 2.35, 2.20, 6.76),
  xend = c(3.87, 10.75, 6.82, 10.11, 3.67, 4.28, 4.99, 2.69, 7.01, 4.90, 8.64,
           1.48, 0.20, 0.32, 2.16, 4.55),
  colour = c("#97233f", "#241773", "#0b162a", "#fb4f14", "#002244", "#002244",
            "#203731", "#000000", "#002244", "#008e97", "#4f2683", "#203731",
            "#004953", "#000000", "#002244", "#aa0000")
)

ggplot(ex_data)  
  geom_link(aes(x = x, xend = xend, y = id, yend = id, colour = colour,
                alpha = stat(index), size = stat(index)), show.legend = FALSE)  
  scale_colour_manual(values = ex_data$colour)

CodePudding user response:

Perhaps like this?

ggplot(ex_data)  
  geom_link(aes(x = x, xend = xend, y = id, yend = id, colour = colour,
                alpha = stat(index)), show.legend = FALSE, size = 5, n = 500)  
  scale_colour_identity()

You had size varying but it sounds like you want it constant. I also increased n to make the color shift smoother, and switched to scale_colour_identity since I think you want the color to be assigned based on the color assigned to that row, which might not be the same as the order of appearance in the original data.

enter image description here

  • Related