I know how to split and fill areas of a polygon along a horizontal line, if the values are quite simple.
x <- 9:15
y1 <- c(5, 6, 5, 4, 5, 6, 5)
plot(x, y1, type="l")
abline(h=5, col="red", lty=2)
polygon(x[c(1:3, 5:7)], y1[c(1:3, 5:7)], col="green")
polygon(x[3:5], y1[3:5], col="red")
y2 <- c(5, 6, 4, 7, 5, 6, 5)
plot(x, y2, type="l")
abline(h=5, col="red", lty=2)
But how to get the result if the values are a bit more skew?
Expected output (photoshopped):
As pointed out by #Henrik in comments we can interpolate the missing points.
If the data is centered around another value than zero – as in my case – we need to adapt the method a little.
x <- 9:15
y2 <- c(5, 6, 4, 7, 5, 6, 5)
zp <- 5 # zero point
d <- data.frame(x, y=y2 - zp) # scale at zero point
# kohske's method
new_d <- do.call(rbind,
sapply(1:(nrow(d) - 1), function(i) {
f <- lm(x ~ y, d[i:(i + 1), ])
if (f$qr$rank < 2) return(NULL)
r <- predict(f, newdata=data.frame(y=0))
if(d[i, ]$x < r & r < d[i + 1, ]$x)
return(data.frame(x=r, y=0))
else return(NULL)
})
)
d2 <- rbind(d, new_d)
d2 <- transform(d2, y=y + zp) # descale
d2 <- unique(round(d2[order(d2$x), ], 4)) # get rid of duplicates
# plot
plot(d2, type="l")
abline(h=5, col="red", lty=2)
polygon(d2$x[c(1:3, 5:9)], d2$y[c(1:3, 5:9)], col="green")
polygon(d2$x[3:5], d2$y[3:5], col="red")
Result
Related
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.
I have 2 data sets (DSA and DSB) that contain x & y coordinates
tumor<- data.frame(DSA[,c("X_Parameter","Y_Parameter")])
cells<-data.frame(DSB[,c ("X_Parameter","Y_Parameter")])
plot(cells, xlim=c(1,1300), ylim=c(1,1000), col="red")
par(new=TRUE)
plot(tumor, xlim=c(1,1300), ylim=c(1,1000), col="blue")
the plots make this graph
I want to be able to draw a connecting line from every red dot to every blue dot.
Does anyone know if this can be done. thanks
Sample
DSA=(5,5 6,6 5,6 6,5) DSB=(1,1 10,10 10,1 1,10)
what the plot should look like
Brute-force, perhaps inelegant:
DSA <- data.frame(x = c(5, 6, 5, 6),
y = c(5, 6, 6, 5))
DSB <- data.frame(x = c(1, 10, 10, 1),
y = c(1, 10, 1, 10))
plot(y ~ x, DSB, col = "red")
points(DSA, col = "blue")
for (r in seq_len(nrow(DSA))) {
segments(DSA$x[r], DSA$y[r], DSB$x, DSB$y)
}
Edit: more directly:
nA <- nrow(DSA)
nB <- nrow(DSB)
plot(y ~ x, DSB, col = "red")
points(DSA, col = "blue")
segments(rep(DSA$x, each = nB), rep(DSA$y, each = nB),
rep(DSB$x, times = nA), rep(DSB$y, times = nA))
(I still can't figure out an elegant solution with #42's recommendation for combn or outer.)
Suppose I want to plot an R function:
weibull <- function(ALPHA, LAMBDA, T){
ALPHA*LAMBDA*(T^(ALPHA-1))
}
So the function takes the arguments alpha, lambda and T. I want to generate a plot where in one plot alpha =0.5, time ranges from 0 to 2 and lambda=1, 2, 4, 8, 16 and in another, alpha=1, time ranges from 0 to 2 and lambda=1, 2, 4, 8, 16.
In the past for plotting functions with just one argument, I've used curve and then done ADD=TRUE if I wanted another curve on the same plot. So for instance, in the past I've used:
lambda <- 0.5
pdf <- function(x){
lambda*exp(-lambda*x)
}
survival <- function(x){
exp(-lambda*x)
}
plot(curve(pdf, 0, 6), type="l", ylim=c(0, 1), lwd=3, ylab="", xlab="", xaxs="i", yaxs="i", main=expression(paste("Exponential Distribution ", lambda, "=0.5")), cex.main=2, cex.axis=2, cex.lab=2)
curve(survival, 0, 6, add=TRUE, col="plum4", lwd=3)
But in this example the functions just have one argument, which is x. Whereas, now I want to vary LAMBDA, T and ALPHA. The curve function does not work and I am not sure how else to approach this.
If you use curve, you can specify an expression with a free variable x that will get replaced by the range of values specified in your from=/to= parameters. For example you can do
weibull <- function(ALPHA, LAMBDA, T){
ALPHA*LAMBDA*(T^(ALPHA-1))
}
lambda<-c(1, 2, 4, 8, 16)
col<-rainbow(length(lambda))
layout(matrix(1:2, nrow=1))
for(i in seq_along(lambda)) {
curve(weibull(.5, lambda[i], x), from=0, to=2, add=i!=1, col=col[i], ylim=c(0,50), main="alpha=.5")
}
legend(1,50,lambda, col=col, lty=1)
for(i in seq_along(lambda)) {
curve(weibull(1, lambda[i], x), from=0, to=2, add=i!=1, col=col[i], ylim=c(0,20), main="alpha=1")
}
which will produce a plot like
I'd do it with plyr and ggplot2,
weibull <- function(alpha, lambda, time){
data.frame(time = time, value = alpha*lambda*(time^(alpha-1)))
}
library(plyr)
library(ggplot2)
params <- expand.grid(lambda = c(1, 2, 4, 8, 16), alpha = c(0.5, 1))
all <- mdply(params, weibull, time = seq(0, 2, length=100))
ggplot(all, aes(time, value, colour=factor(lambda)))+
facet_wrap(~alpha,scales="free", ncol=2) + geom_line()
A tidyverse alternative,
weibull <- function(alpha, lambda, time){
data.frame(time = time, value = alpha*lambda*(time^(alpha-1)))
}
library(ggplot2)
library(tidyverse)
params <- tidyr::crossing(lambda = c(1, 2, 4, 8, 16), alpha = c(0.5, 1))
params %>%
dplyr::mutate(purrr::pmap(., .f = weibull, time = seq(0, 2, length=100))) %>%
tidyr::unnest() %>%
ggplot(aes(time, value, colour=factor(lambda)))+
facet_wrap(~alpha,scales="free", ncol=2) + geom_line()
This is similar to MrFlick's answer but shorter:
par(mfrow=1:2)
lapply(0:4, function(l) curve(weibull(0.5, 2^l, x), col=l+1, add=l!=0, ylim=c(0,50), xlim=c(0,2)))
lapply(0:4, function(l) curve(weibull(1, 2^l, x), col=l+1, add=l!=0, ylim=c(0,50), xlim=c(0,2)))
Ok if you're a big fan of nested lapply's you can also do:
lapply(c(0.5,1), function(a) lapply(0:4, function(l) curve(weibull(a, 2^l, x), col=l+1, add=l!=0, ylim=c(0,50), xlim=c(0,2))))
I have a problem using loess and loess.smooth with a time series with missing data.
Both commands don't work with this toy data.
x <- as.Date(c(1, 2, 4, 5, 6), origin="2010-1-1")
y <- c(4, 8, 8, 28, 11)
plot(x, y, ylim=c(1,30))
lines(loess(y ~ x), col="red")
lines(loess.smooth(y=y, x=x), col="blue")
I ended up using the following code:
# Data
x.1 <- as.Date(c(1, 2, 4, 5, 6), origin="2010-1-1")
x.2 <- c(1, 2, 4, 5, 6)
y <- c(4, 8, 8, 28, 11)
# x.2 - x is numeric variable
plot(x.2, y, ylim=c(1,30))
lines(loess(y ~ x.2, span=1.01), col="black", lwd=2, lty=2) # neccessary to change span default to avoid warnings (span = 0.75)
lines(loess.smooth(x.2, y, span=1.01), col = "orange", , lwd=2) # neccessary to change span default to avoid warnings (span = 2/3)
lines(smooth.spline(x.2,y), col="blue", lwd=2)
# x.1 - x is date variable
plot(x.1, y, ylim=c(1,30))
# loess() cannot deal with date variables, thus convert it to
lines(loess(y~as.numeric(x.1), span=1.01), col="red", lwd=2) # neccessary to change span default to avoid warnings (span = 0.75)
lines(loess.smooth(x.1, y, span=1.01), col = "orange", lwd=2) # neccessary to change span default to avoid warnings (span = 2/3)
lines(smooth.spline(x.1,y), col="blue", lwd=2)
The problems were:
(1) loess is unable to deal with date variables.
(2) The span parameter had to be adjusted (>1).
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: