So as said in the question I want to connect points in ggplot2 graph. The points should be connected one after another in the order they are plotted to the graph, like this :
basepop <- matrix(c(10,30),nrow = 2)
matrixA <- matrix(c(0.4,-0.325,0.3,1.2),nrow = 2)
matrixA
z <- eigen(matrixA)
z$val
eigenmatrix <- z$vec
eigenmatrix
eigeninverse <- solve(eigenmatrix)
constmatrix <- eigeninverse %*% basepop
constmatrix
popdata = NULL
for(k in 1:100){
predator <- c(constmatrix[1,1]*z$val[1]^k*z$vec[1,1] + constmatrix[2,1]*z$val[2]^k*z$vec[1,2])
prey <- c(constmatrix[1,1]*z$val[1]^k*z$vec[2,1] + constmatrix[2,1]*z$val[2]^k*z$vec[2,2])
popdata = rbind(popdata, data.frame(k,predator,prey))
}
library(ggplot2)
ggplot(popdata, aes(predator, prey, color = k)) +
geom_point()
ggplot(popdata, aes(k, predator, color = k)) +
geom_point()
ggplot(popdata, aes(k, prey, color = k)) +
geom_point()
print(popdata)
This is my code for calculating the pop of a prey and predator system.
From @Duck's comment: just add + geom_line()
or + geom_path()
to a plot. ("Which to use" depends on your use: geom_line
connects by the order on the x-axis, geom_path
connects in the order they appear in the data, both have valid uses. In this case they appear to be the same, but keep this in mind if your real data changes.)
ggplot(popdata, aes(predator, prey, color = k)) +
geom_point() +
geom_line()
(Similarly for the other plots.)
I should note that order matters.
# left
ggplot(popdata, aes(predator, prey, color = k)) +
geom_point() +
geom_line(color="black")
# right
ggplot(popdata, aes(predator, prey, color = k)) +
geom_line(color="black") +
geom_point()