rplotggplot2vegan

ggplot2 for procrustes rotation in vegan


I want to plot procrustes rotations between to RDA-objects obtained by vegan with ggplot2.

library(vegan)
#perform two RDAs, do procrustes:
pro.test <- procrustes(rda.t1,rda.t2)

I extracted the x,y coordinates from list of class "procrustes" and added a factor "dates".

test <- data.frame(rda1=pro.test$Yrot[,1], rda2=pro.test$Yrot[,2])
test$dates <- c(rep("A", 8), rep("B",8), rep("C", 8))
test.2 <- data.frame(rda1=pro.test$X[,1], rda2=pro.test$X[,2])
test.2$dates <- c(rep("A", 8), rep("B",8), rep("C", 8))

Now the basic plot:

ggplot() +
geom_point(data=test, aes(x=rda1, y=rda2, color=dates)) +
geom_point(data=test.2,aes(x=rda1, y=rda2, color=dates))

The part i cannot do is the plotting of small lines between each corresponding point in test and test.2 Vegan does plot these rotations with arrows rather than connected points. However, vegan does not khow to color according to sampling groups/factors, which is important for me. Having arrows in the ggplot would be extremely great - i know there is a geom_segment with the argument "arrow".

Could you help me?

the dput of pro.test is below.

    dput(pro.test)
structure(list(Yrot = structure(c(0.126093537705143, 0.196350569855869, 
-0.0513472841582749, -0.304416713452662, 0.210682972922012, -0.0219477831881197, 
-0.24519038499101, 0.338357488742126, -0.399739151138497, -0.366424716631558, 
0.0321561053701086, 0.565794811541598, 0.606054432756139, -0.0122819831669951, 
-0.00403199420346042, -0.0448308879361932, 0.0631101371381566, 
-0.150820933315408, -0.018216051372273, -0.68513841544701, -0.117446131920294, 
-0.450735018917557, 0.25749869839177, 0.47646869541639, -0.211447138648954, 
-0.236584149111598, -0.0316882271224907, -0.281680981927695, 
-0.182346139754316, -0.366221121187894, -0.263915986724565, -0.203160918536977, 
0.209888424862468, 0.219400450315756, 0.143569801341895, 0.258388604988749, 
0.542334722496036, 0.465147580652753, 0.294835945722885, 0.523372408452242, 
0.0739580893460179, 0.242768571724456, 0.0409877673276456, -0.0942111509903291, 
-0.193072299067071, -0.38889179801965, -0.352882980509932, -0.208549475629433
), .Dim = c(24L, 2L), .Dimnames = list(c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "12", "13", "14", "15", "16", 
"17", "18", "19", "20", "21", "22", "23", "24", "25"), NULL)), 
    X = structure(c(0.0860177119127241, 0.217144585357183, -0.0301829830202831, 
    -0.246142550516987, 0.230574651598493, 0.00485065775494245, 
    -0.225907453854864, 0.371465194869491, -0.395330365511425, 
    -0.359255005182027, -0.00775013746753128, 0.47442649486468, 
    0.519983070801763, -0.0146878517934982, 0.0377018407084686, 
    -0.0885829362985767, 0.0935962405791314, -0.186192083265912, 
    0.00247095461296341, -0.655467761687806, -0.0966978065526177, 
    -0.398672122636169, 0.275589258531376, 0.39104839619648, 
    -0.273098318897548, -0.237373845171625, -0.0351119316278201, 
    -0.279271270040404, -0.171188235636994, -0.342350443283954, 
    -0.297148604541773, -0.21965804713297, 0.269830887309913, 
    0.268669489120665, 0.143826114581508, 0.229549645414776, 
    0.531869658831067, 0.479136042616735, 0.380638462867711, 
    0.548249030471031, 0.161449266776772, 0.282765937749097, 
    0.0756433464279055, 0.00516171212969907, -0.195519622624857, 
    -0.568932423412245, -0.381681091857682, -0.375455760069009
    ), .Dim = c(24L, 2L), const = 1.30375728392289, .Dimnames = list(
        c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
        "12", "13", "14", "15", "16", "17", "18", "19", "20", 
        "21", "22", "23", "24", "25"), c("RDA1", "RDA2")), "`scaled:center`" = structure(c(1.3588667228485e-17, 
    4.69820941410385e-18), .Names = c("RDA1", "RDA2"))), ss = 0.146265769408323, 
    rotation = structure(c(-0.958883328045618, 0.283800569407742, 
    0.283800569407742, 0.958883328045619), .Dim = c(2L, 2L)), 
    translation = structure(c(-9.76742606822348e-18, 1.35523649355013e-17
    ), .Dim = 1:2), scale = 0.918742698883168, xmean = structure(c(1.3586408473959e-17, 
    4.71176194125992e-18), .Names = c("RDA1", "RDA2")), symmetric = FALSE, 
    call = procrustes(X = rda.t1, Y = rda.t2), svd = structure(list(
        d = c(2.51563498111738, 2.16729713036852), u = structure(c(0.743008138366833, 
        0.669282381600362, 0.669282381600362, -0.743008138366833
        ), .Dim = c(2L, 2L)), v = structure(c(-0.522515395489416, 
        0.852629850214347, -0.852629850214347, -0.522515395489416
        ), .Dim = c(2L, 2L))), .Names = c("d", "u", "v"))), .Names = c("Yrot", 
"X", "ss", "rotation", "translation", "scale", "xmean", "symmetric", 
"call", "svd"), class = "procrustes")

Solution

  • Does this do the job?

    library(ggplot2)
    library(grid)
    
    ctest <- data.frame(rda1=pro.test$Yrot[,1],
    rda2=pro.test$Yrot[,2],xrda1=pro.test$X[,1],
    xrda2=pro.test$X[,2],dates=rep(c("A","B","C"),each=8))
    
    ggplot(ctest) +
    geom_point(aes(x=rda1, y=rda2, colour=dates)) +
    geom_point(aes(x=xrda1, y=xrda2, colour=dates)) +
    geom_segment(aes(x=rda1,y=rda2,xend=xrda1,yend=xrda2,colour=dates),arrow=arrow(length=unit(0.2,"cm")))