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.)
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 tried to plot 6 normal distribution figure to display the effect of mean and variance on such a plot,my code is as follow:
par(mfrow=c(3,2),bty = "n") # 3 rows by 2 columns, turn off border
mu <- c(6, 8, 6, 8, 6, 8) #designate the 6 mean values
sigma <- c(3, 3, 2, 2, 1, 1) #designate the 6 sd values
label <- c("(a)","(b)","(c)","(d)","(e)","(f)") #designate the 6 labels of the 6 figures
for(i in 1:length(mu))
{
mu.r <- mu[i]
sigma.r <- sigma[i]
lab.r <- label[i]
x <- seq((mu.r - 4*sigma.r), (mu.r + 4*sigma.r), len = 200)
#designate the starting and ending value of mean
plot(x, dnorm(x, mean = mu.r, sd = sigma.r),axes = F,
type="l",lwd = 2, xlab = lab.r, ylab = "",
main=paste0('mu=',mu.r,', sigma=',sigma.r),
)
axis(1, at = (mu.r - 4*sigma.r) : (mu.r + 4*sigma.r))
abline(v = mu.r, col = "red", lwd = 2.5, lty = "longdash")
}
the figures generated is as follow:
[enter image description here][1]
[1]: https://i.stack.imgur.com/Z4czh.png
You didin't said what exactly was the problem. I assume is that all your graphs look the same. That happens because you set your x axis depending on the variance, you need to leave all the graphs on the same scale in order to compare them. I simply set a arbitrary interval of 7 around the mean:
for(i in 1:length(mu))
{
mu.r <- mu[i]
sigma.r <- sigma[i]
lab.r <- label[i]
x <- (mu.r - 7):(mu.r + 7)
#designate the starting and ending value of mean
plot(x, dnorm(x, mean = mu.r, sd = sigma.r),axes = F,
type="l",lwd = 2, xlab = lab.r, ylab = "",
main=paste0('mu=',mu.r,', sigma=',sigma.r),
)
axis(1, at = x)
abline(v = mu.r, col = "red", lwd = 2.5, lty = "longdash")
}
Output:
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)
I'm trying to calculate derivatives of Gaussians in R and when I try to specify the mean and standard deviation, R seems to ignore this. For example, the following code works to plot a N(0,1) density and it's first and second derivative.
st_norm <- function(x) dnorm(x,0,1)
first_deriv <- function(x) {}
second_deriv <- function(x) {}
body(first_deriv) <- D(body(st_norm), 'x')
body(second_deriv) <- D(body(first_deriv), 'x')
curve(st_norm, -4, 4, ylim = c(-0.4, 0.4), col = 'blue')
curve(first_deriv, -4, 4, add = T, col = 'red')
curve(second_deriv, -4, 4, add = T, col = 'green')
abline(v=0, h=0)
and this produces the following plot:
But, suppose I wanted to do the exact same but to a N(2,2), then I change the code accordingly to:
different_norm <- function(x) dnorm(x,2,2)
different_first_deriv <- function(x) {}
different_second_deriv <- function(x) {}
body(different_first_deriv) <- D(body(different_norm), 'x')
body(different_second_deriv) <- D(body(different_first_deriv), 'x')
curve(different_norm, -4, 8, ylim = c(-0.4, 0.4), col = 'blue')
curve(different_first_deriv, -4, 8, add = T, col = 'red')
curve(different_second_deriv, -4, 8, add = T, col = 'green')
abline(v=0, h=0)
which produces this plot:
so you can see that it is taking the derivatives of a standard normal, not a N(2,2). If you print out the functions of first_deriv and different_first_deriv, they are equal, even though they are meant to be differentiating different functions.
Does anyone know how to fix this problem so I'm taking derivatives of the specified Gaussian distribution I want?
It works if you use a different formula e.g. try this:
different_norm <- function(x, mean=2, sd=2) dnorm((x-mean)/sd, 0, 1)/sd
If you see the General normal distribution section of https://en.wikipedia.org/wiki/Normal_distribution#Alternative_parameterizations then its a re-parameterisation of the standard normal.
I assume the issue is that the parameters mean and sd don't appear in the formula, and additional args from dnorm aren't passing down for some reason
You can just calculate the derivatives and write it out yourself
dnorm_deriv1 <- function(x, mean = 0, sd = 1) {
return(-((x-mean)/(sd^2))*dnorm(x, mean, sd))
}
dnorm_deriv2 <- function(x, mean = 0, sd = 1) {
return((((x-mean)^2) / (sd^4))*dnorm(x, mean, sd)
- (1/sd^2)*dnorm(x, mean, sd))
}
curve(dnorm, -4, 4, ylim = c(-0.4, 0.4), col = 'blue')
curve(dnorm_deriv1, -4, 4, add = T, col = 'red')
curve(dnorm_deriv2, -4, 4, add = T, col = ' green')
abline(v=0, h=0)
curve(dnorm(x, 2, 2), -4, 8, ylim = c(-0.1, 0.2), col = 'blue')
curve(dnorm_deriv1(x, 2, 2), -4, 8, add = T, col = 'red')
curve(dnorm_deriv2(x, 2, 2), -4, 8, add = T, col = ' green')
abline(v=2, h=0)
set.seed(2)
x = c(1, rnorm(100, 15, 5))
y = c(1, rnorm(100, 15, 5))
plot(x, y, log = "xy")
plot(log(x), log(y))
What is the difference when i use log-argument in plot() function and when i transform the variables first, then plot them. Hier is the plot with plot(x, y, log = "xy").
And with plot(log(x), log(y))
Can someone explain me what does the log-argument exactly do? Thank you.
For log-plots you do not have to modify your data. It is just a matter of displaying, so that you can further work with your data. Just as example:
set.seed(2)
x = c(1, rnorm(100, 15, 50))
y = c(1, rnorm(100, 15, 50))
plot(x, y, col = "black",
log = "xy", xaxt = "n", yaxt = "n",)
# Labels...
at.y <- outer(1:9, 10^(log10(1):log10(100)))
lab.y <- ifelse(log10(at.y) %% 1 == 0,
sapply(at.y, function(i)
as.expression(bquote(10^.(log10(i))))
), NA)
axis(2, at = at.y, labels = lab.y, las = 1)
at.x <- outer(1:9, 10^(0:log10(100)))
lab.x <- ifelse(log10(at.x) %% 1 == 0,
sapply(at.x, function(i)
as.expression(bquote(10^.(log10(i))))
), NA)
axis(1, at = at.x, labels = lab.x, las = 1)
grid (NULL,NULL, lty = 6, col = "cornsilk2")
Result: