1
votes

I am using ggplot2 in R to create scatterplots. I have (x,y) datasets for two different conditions as follows:

    x1           y1           x2             y2
1   1.00000000  150.36247   0.50000000  133.27397
2   1.00000000  129.62707   0.50000000  120.79893
3   1.00000000  79.94730    0.62500000  78.98120
4   1.00000000  79.78723    0.62500000  81.93014
5   1.00000000  133.47697   0.72727273  192.86557

I would like to plot (x1,y1) and (x2, y2) on the same plot, and draw a trajectory line connecting the two points, for each row in the dataset. For example, in the sample data above, there would be a line connecting (1,150) to (0.5, 133) (the data from row 1) and a separate line connecting (1,129) and (0.5, 120) (the data from row 2) and so on. Ideally, I would also like to make each line a different color.

I tried following the instructions below to create the trajectories but the grouping in my dataset is by columns rather than by rows: Scatterplot with developmental trajectories, and Making a trajectory plot using R

Currently, my script just generates two scatterplots on the same graph but there is no connection between data points of the same row:

scatter2<-ggplot(data = df, aes(x=x1, y=y1))
+ geom_point(aes(x=x1, y=y1), color="red") 
+ geom_point(aes(x=x2, y=y2), color="blue") 

Any help you can provide would be greatly appreciated! Thank you!

2

2 Answers

1
votes

This can be achieved by using geom_segment(). You can find more information here: http://docs.ggplot2.org/current/geom_segment.html.

I suppose here, you would do something like

scatter2<-ggplot(data = df, aes(x=x1, y=y1))
  + geom_point(aes(x=x1, y=y1), color="red") 
  + geom_point(aes(x=x2, y=y2), color="blue") 
  + geom_segment(aes(x=x1, y=y1, xend=x2, yend=y2))
1
votes
DF <- read.table(text="    x1           y1           x2             y2
1   1.00000000  150.36247   0.50000000  133.27397
2   1.00000000  129.62707   0.50000000  120.79893
3   1.00000000  79.94730    0.62500000  78.98120
4   1.00000000  79.78723    0.62500000  81.93014
5   1.00000000  133.47697   0.72727273  192.86557", header=TRUE)

Tidy up your data:

DF$id <- seq_len(nrow(DF))
library(reshape2)
DF <- melt(DF, id.var="id")
DF$var <- substr(DF$variable, 1, 1)
DF$group <- substr(DF$variable, 2, 2)
DF$variable <- NULL
DF <- dcast(DF, group + id ~ var)

Plot:

library(ggplot2)
ggplot(DF, aes(x=x, y=y)) +
  geom_point(aes(shape=group), size=5) +
  geom_line(aes(colour=factor(id)))

resulting plot