I have two matrices of same size.
x1 = matrix(data = c(1, 3, 4, 5, 5, 3, 3, 1), nrow = 4, ncol= 2, byrow = TRUE)
x2 = matrix(data = c(-1, -4, 3, 7, -2, 2, 4, -1), nrow = 4, ncol= 2, byrow = TRUE)
I want to plot the both on the same scatter plot, however, x should contain all 'x' values from both x1 and x2, and y also should contain all 'y' values from both matrices.
Matplot doesn't seem to do the work, since it only compares the columns.
How can I do this (if possible without using any packages)?
You can use plot
plot(x1, xlim = c(-3, 6), ylim = c(-5, 7), col = "red", xlab = "X", ylab = "Y")
par(new=TRUE)
plot(x2, xlim = c(-3, 6), ylim = c(-5, 7), col = "blue", xlab = "", ylab = "")
We can use pairs
pairs(cbind(c(x1), c(x2)))
Or just remove the dim attributes in 'x1', 'x2' with c to convert to vector and use plot
plot(c(x1), c(x2))
(reproducible code given) I am studying Ugarte2016's "Probability and Statistics with R" 2E. The following code is run in R but Latex-like code is not processed. It seems that the code inside "$...$" is not processed. The code supplied below was from the authors of the book. There seems a problem somehow. What could be the problem?
######### Chapter 12 #############
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
################ Figure 12.1 ###############
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", xlim = c(0, 12), ylim = c(-2, 12), xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10), labels = c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$", "$E(Y|x_1) = \\beta_0 + \\beta_1x_1$", "$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \\beta_0 + \\beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
The code result:
The image in the book:
Use package latex2exp:
######### Chapter 12 #############
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
library(latex2exp)
################ Figure 12.1 ###############
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", xlim = c(0, 12), ylim = c(-2, 12), xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), labels = TeX(c("$x_1$", "$x_2$", "$x_3$")))
axis(side =2, at =c(1, 4, 10), labels = TeX(c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$", "$E(Y|x_1) = \\beta_0 + \\beta_1x_1$", "$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") ))
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, TeX("$E(Y|x) = \\beta_0 + \\beta_1x$"))
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
All graphs in the book were created running the knitr option dev = "tikz"...specifically for the graph in question:
<<c12slrModFIG, echo = FALSE, dev = "tikz", crop = TRUE, fig.align = 'center', results = 'hide', fig.height = 5, fig.width = 7, out.width='0.95\\linewidth', warning = FALSE>>=
The solution of sandipan uses latex2exp::TeX. There is a solution that keeps the original code and does not use latex2exp::TeX at all.
When I contacted the authors of the book, they generously sent a code and specified that they used tikzDevice and knitr to produce the graphs. Being novice to both knitr/tkizDevice, I found a way to obtain the image just as in the book (italic LateX'ed chars on the plot); I am sure there must be a better approach:
The tikzDeviceAndKnitr.Rnw file is put in R's working directory (one may find it via getwd()).
tikzDeviceAndKnitr.Rnw:
<<PASWR2fCh12S1, echo=FALSE, dev="tikz", crop=TRUE, fig.align='center', results='hide', fig.height=5, fig.width=7, out.width='0.95\\linewidth', warning=FALSE>>=
library(tikzDevice)
tikz('tikzDeviceAndKnitr.tex', standAlone=TRUE, width=5, height=5)
opar <- par(no.readonly = TRUE)
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n",
xlim = c(0, 12), ylim = c(-2, 12),
xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10),
labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),
labels = c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \\beta_0 + \\beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
dev.off()
tools::texi2dvi('tikzDeviceAndKnitr.tex',pdf=T)
system(paste(getOption('pdfviewer'), 'tikzDeviceAndKnitr.pdf'))
#
In MikTeX of Windows, install packages related with tikz and pgf.
Load the libraries in R and knit the related .Rnw file:
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
library(latex2exp); library(knitr);library(tikzDevice);library(tools)
library(evaluate); library(markdown)
knit("tikzDeviceAndKnitr.Rnw") # The solution ended.
The book's author's reply to me is:
Yes....tikzDevice is used with knitr. The complete code looks like:
\begin{figure}[!ht]
<<c12slrModFIG, echo = FALSE, dev = "tikz", crop = TRUE, fig.align = 'center', results = 'hide', fig.height = 5, fig.width = 7, out.width='0.95\\linewidth', warning = FALSE>>=
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n",
xlim = c(0, 12), ylim = c(-2, 12),
xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10),
labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),
labels = c("$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$",
"$E(Y|x_1) = \\beta_0 + \\beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \\beta_0 + \\beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
#
\caption{Graphical representation of simple linear regression model
depicting the distribution of $Y$ given x \label{SLRgraph}}
\end{figure}
While plotting a raster image, for example:
library(raster)
r <- raster(nrow = 3, ncol = 3)
values(r) <- 1:9
plot(r, col = terrain.colors(255))
How can I get the legend being in ascending order, i.e. from 1 (top) to 9 (bottom)?
I thought of the legend.args, but couldn't find the right arguments.
I tried a bit and I think I've found a solution myself, even though it is not the most elegant way.
library(raster)
r <- raster(nrow = 3, ncol = 3)
values(r) <- 1:9
par(mar = c(3, 3, 4, 3))
plot(r, col = terrain.colors(255),legend = FALSE, xlim = c(-200,200),
ylim = c(-200,200))
vz = matrix(1:100, nrow = 1)
vx = c(185, 195)
vy = seq(-10, 10, length.out = 100)
par(new = TRUE, mar = c(3, 3, 4, 3))
plot(1, xlab = "", ylab = "", axes = FALSE, type = "n",
xlim = c(-200, 180), ylim = c(-20, 20))
image(vx, vy, vz, col = rev(terrain.colors(255)), axes = FALSE,
xlab = "", ylab = "", add = TRUE)
polygon(c(185, 195, 195, 185), c(-10, -10, 10, 10))
axis(4, at = seq(-10, 10, length.out = 9), labels = 9:1, las = 1)
Anyway, I'd appreciate other ideas!
I have code producing the below pasted plot
x <- c(2, 3, 4)
y <- c(2.5, 4.1, 5.5)
plot(x, y, type = "o", xlim = c(1, 5), ylim = c(2, 6), axes = FALSE, bty = "n")
axis(side = 1, at = seq(1, 5, 1))
axis(side = 2, at = seq(2, 6, 1), las = 2)
I would like to have neither ticks nor labels at position 1 and 5, but the axis should still be drawn. This is what I am looking for:
When using labels = c("", 2, 3, 4, "") ticks are drawn. When using tick = FALSE, I get no axis. Does anybody have a solution for this?
You just need to draw the line manually. Using the line2user function in this answer:
x <- c(2, 3, 4)
y <- c(2.5, 4.1, 5.5)
plot(x, y, type = "o", xlim = c(1, 5), ylim = c(2, 6), axes = FALSE, bty = "n")
axis(side = 1, at = 2:4)
lines(x = c(1, 5), y = rep(line2user(0, side = 1), 2), xpd = TRUE)
axis(side = 2, at = seq(2, 6, 1), las = 2)
Note, the line2user function just gives the position of the lines in the user coordinate system. You need the xpd = TRUE to draw outside the plotting region.