Plot a system of equations in R - r

Suppose you have a system of equations (5 equations and 2 variables) that look like this:
Ax + By = C
AB <- matrix(runif(10), 5)
C <- c(5, 10, 15, 20, 25)
How do you plot this system of equations in R, without first manually converting into
y = (C - Ax) / B

Are you looking for something like this?
set.seed(101)
AB <- matrix(runif(10), 5)
C <- c(5, 10, 15, 20, 25)
x = seq(-70,70,1)
plot(x, (C[1] - AB[1,1]*x)/AB[1,2], col=1, type="l", ylim=c(-100,200))
for (i in 2:nrow(AB)) {
lines(x, (C[i] - AB[i,1]*x)/AB[i,2], col=i, type="l")
}

Related

Plot+points animation works in graphics device but only saves as single GIF image, not video

Using the "animate" package to create plot videos. I need to be able to create an initial plot and then add multiple series with a delay in between. As an example, the following code works great in the R graphics device:
library(animate)
x <- rnorm(20, 5, 2)
rand <- rnorm(20, 2, 3)
y <- x + rand
df <- data.frame(a = rnorm(20, 6, 2), b = rnorm(20, 3, 2), c = rnorm(20, 10, 3))
co <- c("red", "blue", "orange")
oopt = ani.options(interval = 2)
plot(y, x, col="green")
points(y, x, type='l', col="green", lwd=3)
ani.pause()
interval = ani.options('interval')
for (i in 1:3){
newy <- df[,i]
points (x, newy, col=co[i])
ani.pause()
}
However, this same code when placed within saveGIF will save only a static image instead of a video:
x <- rnorm(20, 5, 2)
rand <- rnorm(20, 2, 3)
y <- x + rand
df <- data.frame(a = rnorm(20, 6, 2), b = rnorm(20, 3, 2), c = rnorm(20, 10, 3))
co <- c("red", "blue", "orange")
oopt = ani.options(interval = 2)
saveGIF({
plot(y, x, col="green")
points(y, x, type='l', col="green", lwd=3)
ani.pause()
interval = ani.options('interval')
for (i in 1:3){
newy <- df[,i]
points (x, newy, col=co[i])
ani.pause()
}
},movie = "savegiftest.gif")
Tried sending additional parameters to image magick with no success so far (I noted in the animate documentation that perhaps the issue is that ani.pause may not work, however, interval in image magick doesn't work either). I'm open to saving in other video formats as well.

R: adding axis titles to non ggplot objects

I am working with the R programming language. Normally when I make plots, I am using the ggplot2 library and the aes() options can be used to label the x-axis and add a title. However this time, I the plots I am making are not ggplot2 objects, and therefore can not be labelled in the same way:
library(MASS)
library(plotly)
a = rnorm(100, 10, 10)
b = rnorm(100, 10, 5)
c = rnorm(100, 5, 10)
d = matrix(a, b, c)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
#error - this is also apparent because the ggplotly() command can not be used.
ggplotly(d)
Does anyone know how to add labels on the x-axis of this plot and some title? Can the ggplotly command be used here?
Thanks
You can use title(), e.g.
library(MASS)
a = rnorm(100, 10, 10)
b = rnorm(100, 10, 5)
c = rnorm(100, 5, 10)
d = matrix(a, b, c)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 5, 0.1),
tick = TRUE, las = 1)

Conway Maxwell Distribution Density Plot

I have written my own code to simulate the Conway maxwell distribution sample.
This is the pmf (Guikema & Goffelt, 2008):
However, I have met some problem to plot the density plot.
rcomp <- function(n,lamb,v)
{
u <- runif(n)
w <- integer(n)
for(i in 1:n) {
z=sum(sapply( 0:100, function(j) (( ((lamb)^j) / (factorial(j)) )^v) ))
x <- seq(1, 50, 1) #seq of 1 to 50, increase by 1
px <- (((lamb^x)/factorial(x))^v)/z
# px is pmf of re-parameter conway maxwell
w[i] <- if (u[i] < px[1]) 0 else (max (which (cumsum(px) <= u[i])))
}
return (w)
}
dcomp <- function(x,lamb,v) {
z=sum(sapply( 0:100, function(j) (( ((lamb)^j) / (factorial(j)) )^v) ))
px <- (((lamb^x)/factorial(x))^v)/z
return(px)
}
As I wanna plot the density plot to check whether lamb or v is location parameter, the plot I get is weird.
x = rcomp(100,6,0.2); pdf = dcomp(x,6,0.2)
x1 = rcomp(100,6,0.5); pdf1 = dcomp(x1,6,0.5)
x2 = rcomp(100,6,0.7); pdf2 = dcomp(x2,6,0.7)
plot(x2, pdf2, type="l", lwd=1,lty=1,col="blue")
How could I solve this problem?
Source: Guikema & Goffelt (2008), A Flexible Count Data Regression Model for Risk Analysis. Risk Analysis 28(1): 215.
You have to sort the values of the x coordinate if you want a graph to connect the points in their axis order.
Note, however, that there might be better ways to graph the density you want. See the red curve. I first create a vector x of values within a certain range and then compute the PDF for those values. These pairs (x, y) are what function lines plots.
set.seed(2673) # Make the results reproducible
x2 <- rcomp(100, 6, 0.7)
x2 <- sort(x2)
pdf2 <- dcomp(x2, 6, 0.7)
plot(x2, pdf2, type = "l", lwd = 1, lty = 1, col = "blue")
x <- seq(0, 50, length.out = 100)
y <- dcomp(x, 6, 0.2)
lines(x, y, type = "l", col = "red")

Overlay many plots with a different range of x

I would like to make a plot like the this image what I want, however I don't know how. I wrote the code below but I don't find a way to obtain the plot. The point here is to add density lines to my original plot (Relation Masa-SFR) the density is supposed to be every 0.3 in x. I mean one line from 7 to 7.3, the next one from 7.3 to 7.6 and so on. With the code below (continue until x=12), I obtain the this [plot][2]
plot(SFsl$MEDMASS, SFR_SalpToMPA,xlim= range(7:12),
ylim= range(-3:2.5),ylab="log(SFR(M(sun)/yr)",
xlab="log(M(star)/(M(sun)")
title("Relacion Masa-SFR")
par(new=TRUE)
FCUTsfrsl1=(SFsl$MEDMASS >= 7 & SFsl$MEDMASS <=7.3 &
SFR_SalpToMPA < 2 & SFR_SalpToMPA > -3)
x <- SFR_SalpToMPA[FCUTsfrsl1]
y <- density(x)
plot(y$y, y$x, type='l',ylim=range(-3:2.5), col="red",
ylab="", xlab="", axes=FALSE)
I did what you said but I obtained this plot, I don't know if I did something wrong
Since I don't have your data, I had to make some up. If this does what you want, I think you can adapt it to your actual data.
set.seed(7)
x <- runif(1000, 7, 12)
y <- runif(1000, -3, 3)
DF <- data.frame(x = x, y = y)
plot(DF$x, DF$y)
# Cut the x axis into 0.3 unit segments, compute the density and plot
br <- seq(7, 12, 0.333)
intx <- cut(x, br) # intervals
intx2 <- as.factor(cut(x, br, labels = FALSE)) # intervals by code
intx3 <- split(x, intx) # x values
inty <- split(y, intx2) # corresponding y values for density calc
for (i in 1:length(intx3)) {
xx <- seq(min(intx3[[i]]), max(intx3[[i]]), length.out = 512)
lines(xx, density(inty[[i]])$y, col = "red")
}
This produce the following image. You need to look closely but there is a separate density plot for each 0.3 unit interval.
EDIT Change the dimension that is used to compute the density
set.seed(7)
x <- runif(1000, 7, 12)
y <- runif(1000, -3, 3)
DF <- data.frame(x = x, y = y)
plot(DF$x, DF$y, xlim = c(7, 15))
# Cut the x axis into 0.3 unit segments, compute the density and plot
br <- seq(7, 12, 0.333)
intx <- cut(x, br) # intervals
intx2 <- as.factor(cut(x, br, labels = FALSE)) # intervals by code
intx3 <- split(x, intx) # x values
inty <- split(y, intx2) # corresponding y values
# This gives the density values in the horizontal direction (desired)
# This is the change, the above is unchanged.
for (i in 1:length(intx3)) {
yy <- seq(min(inty[[i]]), max(inty[[i]]), length.out = 512)
offset <- min(intx3[[i]])
lines(density(intx3[[i]])$y + offset, yy, col = "red")
}
Which gives:

Adding a plane to a scatterplot3d

I have an equation of a line
y=sqrt(c+x^2)
and I want to add a plane to a 3d scatterplot, such that my plane is perpendicular to the x y plane and the line given above is the intersection line of the two planes.
How do I do this? I don't quite understand how plane3d works. I've read http://svitsrv25.epfl.ch/R-doc/library/scatterplot3d/html/scatterplot3d.html
But still don't get it.
This might be what you are looking for:
library(scatterplot3d)
# y=sqrt(a+x^2) with x in (-0.5,0.5), z in (0,1) and a=0
a <- 0
x <- rep(seq(-0.5, 0.5, length = 200), each = 200)
y <- sqrt(a + x^2)
z <- rep(seq(0, 1, length = 200), 200)
scatterplot3d(x, y, z, highlight.3d = TRUE, pch = 20)
Edit: That would be helpful to see how did you add these other points, but let us take the second example from ?scatterplot3d
temp <- seq(-pi, 0, length = 50)
x2 <- c(rep(1, 50) %*% t(cos(temp)))
y2 <- c(cos(temp) %*% t(sin(temp)))
z2 <- c(sin(temp) %*% t(sin(temp)))
Now combining x with x2 and doing the same with others we get:
scatterplot3d(c(x,x2), c(y,y2), c(z,z2), highlight.3d = TRUE, pch = 20)
In addition to the previous answer, once you construct a 3-D scatterplot, you can add a plane to it by creating a model and parsing it using a function nested within your scatterplot3d() container. It should look something like this:
plot3d <- scatterplot3d(x, y, z, ... )
model <- lm(y ~ sqrt(c + x^2) + z)
plot3d$plane3d(model)
It's a very weird syntax to have a function within a container like that, but it works, giving you something like this (the dotted-line plane is visible near the center of the cube):
If you would like to create one or multiple planes manually, I would use Uwe's method that I re-posted here:
spd <- scatterplot3d(1:10, 1:10, 1:10)
# xy
spd$plane3d(0.3549896,0,0,lty="dotted")
# yz
x0 <- 5
xyz1 <- spd$xyz.convert(rep(x0, 6), rep(0, 6), seq(0, 10, by=2))
xyz2 <- spd$xyz.convert(rep(x0, 6), rep(10, 6), seq(0, 10, by=2))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
xyz1 <- spd$xyz.convert(rep(x0, 6), seq(0, 10, by=2), rep(0, 6))
xyz2 <- spd$xyz.convert(rep(x0, 6), seq(0, 10, by=2), rep(10, 6))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
# zx
y0 <- 6
xyz1 <- spd$xyz.convert(rep(0, 6), rep(y0, 6), seq(0, 10, by=2))
xyz2 <- spd$xyz.convert(rep(10, 6), rep(y0, 6), seq(0, 10, by=2))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
xyz1 <- spd$xyz.convert(seq(0, 10, by=2), rep(y0, 6), rep(0, 6))
xyz2 <- spd$xyz.convert(seq(0, 10, by=2), rep(y0, 6), rep(10, 6))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
This produces planes through manual specification:

Resources