0

Is there a way to generate a line for each ID across 3 facets? [X1,Y1],[X2,Y2],[X3,Y3] data points for each id (n=30) should be connected using a line. Note the Y-axis range is the same for all 3 plots, but the X-axis differs for V=1, V=2 and V=3. Any other suggestions to visualize this plot using a line for each id?

library(ggplot2)
d <- data.frame(
  ID = rep(1:30,each=3),
  V = rep(1:3,times=30),
  X = c(26,48,180,26,48,172,20,42,179,25,49,149,28,64,188,24,40,136,
        22,58,191,28,47,193,28,58,165,22,45,120,27,46,171,21,39,177,
        23,48,179,21,83,195,21,41,154,18,46,167,31,47,263,23,40,173,
        22,46,161,28,64,102,37,49,240,28,47,171,28,43,154,27,45,175,
        27,45,219,28,64,209,19,43,174,32,38,170,23,46,203,25,68,320
  ),
  Y = c(0.0974,0.9654,0.1904,0.0791,0.5490,0.1926,0.9501,0.6193,0.6041,
        0.1171,0.5914,0.4014,0.2525,0.7176,0.1071,0.2122,1.5051,0.4629,
        0.9501,-0.0961,0.04911,0.6511,0.4741,0.1461,0.1009,0.6974,-0.1115,
        0.6624,0.6242,0.5072,0.6655,0.7116,0.6499,0.9201,0.9979,0.0994,
        0.0606,0.9944,0.1272,0.9159,0.0994,-0.4942,0.9729,0.1551,-0.4496,
        0.5097,-0.0162,-0.1992,0.1701,0.1760,-0.0555,0.6117,0.1299,-0.5112,
        -0.0915,-0.9127,-0.5512,0.5527,0.6665,0.4409,1.0112,0.1071,-0.5512,
        0.5152,-0.1907,-0.1416,-0.0409,0.5415,0.9292,0.5751,0.9658,0.0159,
        0.4223,-0.1416,-0.1204,1.0159,0.7116,0.1992,0.1952,0.9010,0.1642,
        0.9944,0.6511,0.9201,0.4200,0.4121,0.1760,0.5200,0.1121,0.0760
  )
)

d_plot <- ggplot(d, aes(x=X, y=Y, group=ID, color=as.factor(ID))) +
  geom_point() +
  facet_wrap(~V, ncol=3, scales = "free_x") +
  theme_minimal()
d_plot

enter image description here

DB5
  • 31
  • 4
  • 3
    Don't use facets and this will be trivial. You can use `annotate` to put the 1, 2, 3, labels at the top. – Gregor Thomas Aug 30 '23 at 18:45
  • 3
    Appears to duplicate this Q&A https://stackoverflow.com/questions/76813306/draw-lines-between-facets-of-facet-grid – IRTFM Aug 30 '23 at 19:03

0 Answers0