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))
Related
Suppose I make a plot involving the following lines of code:
#Sample Code
calc1 <- 1/4
a <- paste(calc1, "X", sep = "")
calc2 <- (1/2)*100
b <- paste(calc2, "%", sep = "")
x <- c(1, 2, 3)
y <- c(4, 5, 6)
#Plot (Regular Axis Titles)
plot(x, y,
xlim = c(1, 3),
xlab = a,
ylim = c(4, 6),
ylab = b)
Is there a way to make bold axes titles while directly calling vectors having the paste() function? Here is the workaround I made:
#Plot (Bold Axis Titles, Workaround)
plot(x, y,
xlim = c(1, 3),
xlab = expression(bold("0.25X")),
ylim = c(4, 6),
ylab = expression(bold("50%")))
Here are the failed attempts:
#Attempt 1
plot(x, y,
xlim = c(1, 3),
xlab = expression(bold(a)),
ylim = c(4, 6),
ylab = expression(bold(b)))
#Attempt 2
plot(x, y,
xlim = c(1, 3),
xlab = expression(bold(paste(calc1, "X", sep = ""))),
ylim = c(4, 6),
ylab = expression(bold(paste(calc2, "%", sep = ""))))
And here is why I would prefer not to manually encode the returned elements of vectors for axis labelling prior to using expression(bold()):
You can set bold axis titles by use of par(font.lab = 2).
calc1 <- 1/4
a <- paste(calc1, "X", sep = "")
calc2 <- (1/2)*100
b <- paste(calc2, "%", sep = "")
x <- c(1, 2, 3)
y <- c(4, 5, 6)
par(font.lab = 2)
plot(x, y,
xlim = c(1, 3),
xlab = paste(calc1, "X", sep = ""),
ylim = c(4, 6),
ylab = paste(calc2, "%", sep = ""))
I have the following contour plot
x <- c(0,25,50,75,100)
y <- c(0,10,20)
z <- matrix(c(12,12,13,12,5,12,5,5,5,12,5,12,13,14,15), nrow = 5, ncol = 3, byrow = TRUE)
A <- matrix(seq(0, 100, by = 25), nrow = 3, ncol = 5, byrow = TRUE) #As x
B <- matrix(seq(0,20, by = 10), nrow = 3, ncol = 5) #As y
filled.contour(x,y,z, color=terrain.colors,#
plot.axes = { axis(1); axis(2); points(A,B)})
How can I draw a level line around the level with value 5 and label it and obtain something like:
You may use contour in plot.axes. It's not possible to add the line at exactly 5, so I used 5.01 instead and specified labels. This is at least the principle.
filled.contour(x, y, z, color = terrain.colors,
plot.axes = {axis(1); axis(2); points(A, B);
contour(x, y, z, levels = 5.01, labels = "5", col = "red", add = TRUE)})
library(fields)
library(emdbook)
x <- c(0,25,50,75,100)
y <- c(0,10,20)
z <- matrix(c(12,12,13,12,5,12,5,5,5,12,5,12,13,14,15), nrow = 5, ncol = 3, byrow = TRUE)
A <- matrix(seq(0, 100, by = 25), nrow = 3, ncol = 5, byrow = TRUE) #As x
B <- matrix(seq(0,20, by = 10), nrow = 3, ncol = 5) #As y
image.plot(x,y,z)
contour(x,y,z,
add=TRUE, lwd=2, cex=2)
How does the fillOddEven parameter work in the polygon function in R? I have tried a couple of examples but can't spot a change:
layout(matrix(1:2,ncol=2))
x <- c(1:9, 8:1)
y <- c(1, 2*(5:3), 2, -1, 17, 9, 8, 2:9)
plot(1:10)
polygon(x, y, col = "orange", lty = 2, lwd = 2, border = "red",fillOddEven = T)
plot(1:10)
polygon(x, y, col = "orange", lty = 2, lwd = 2, border = "red",fillOddEven = F)
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.
I have big data like the following but this just little sample.
pos <- c(1, 3, 5, 8, 10, 12)
start <- c(1,3, 6, 7, 10, 11)
end <- c(5, 6, 9, 9, 13, 12)
Qunatative variable Pos will be Y axis and X axis will be anthor X variable (quantitative). The horizontal bar length for each Pos value is defined by start and end point. For example,line for 1 will start from 1 and end at 3 in x axis.
The following is rough sketch of desired the figure output.
In base R...
plot(pos, type = 'n', xlim = range(c(start, end)), ylim = c(13,0))
grid()
segments(start, pos, end, pos)
To get it more exactly like your figure...
r <- par('usr')
plot(pos, type = 'n', xlim = range(c(start, end)), ylim = c(13.5,0.5), xlab = '',
xaxt = 'n', yaxt = 'n', panel.first = rect(r[1], r[3], r[2], r[4], col = 'goldenrod'))
# abline(h = 1:13, col = 'white')
# abline(v = 1:13, col = 'white')
grid(lty = 1, col = 'white')
axis(1, 1:13, 1:13, cex.axis = 0.8)
axis(2, 1:13, 1:13, las = 1, cex.axis = 0.8)
segments(start, pos + 0.5, end, pos + 0.5, lwd = 2)
Use package ggplot2 with geom_segment to draw the lines.
Start by combining your data into a data.frame, since this the required data structure for ggplot:
dat <- data.frame(
pos = c(1, 3, 5, 8, 10, 12),
start = c(1,3, 6, 7, 10, 11),
end = c(5, 6, 9, 9, 13, 12)
)
Create the plot:
library(ggplot2)
ggplot(dat) +
geom_segment(aes(x=start, y=pos, xend=end, yend=pos), color="blue", size=3) +
scale_y_reverse()