I would like to overlay two plots:
plot1
t1 <- c(0,1,2,3,4,5,6,7,8,9,10)
d1 <- c(0,2,4,6,8,10,12,14,16,18,20)
plot2
t2 <- c(0,1,2,3,4,5)
d2 <- c(1,3,7,8,8,8)
I tried
plot(d1~t1, col="black", type="l")
par(new=T)
plot(d2~t2, col="black", type="l")
But the problem is: in this way, both x axes also overlay each other while x in plot1 is 1:10 and plot2 1:5
You can use lines for the second plot (instead of plot). Furthermore, we scale the x-axis values of the second plot (t2) with 2 (I(2 * t2)).
plot(d1 ~ t1, col="black", type="l", xlim=c(0,10))
lines(d2 ~ I(2 * t2), col="black", type="l", xlim=c(0,5))
In this way, the x-range of the second plot is identical to the x-range of the first one.
Related
I am trying to plot two different graph in one plot. But, the labeling in y axis is overlapping even though is the labeling is same. Here is my data-
data file 1
data file 2
I used the following code-
plot(`2000_svm_movie`,type="o",col="blue",xlab="Training Years",ylab="Performances", axes=FALSE)
axis(1,at=seq(2000,2014,by=1),las=2)
axis(2,at=seq(78,82,by=1),las=1)
par(new = TRUE)
plot(`2000_random_movie`,type="o",col="red",xlab="Training Years",ylab="Performances", axes=FALSE)
axis(1,at=seq(2000,2014,by=1),las=2)
axis(2,at=seq(78,86,by=1),las=1)
I want Y axis will be labeled like (78,79,80,81,82,83,84,85,86). How can I do this?
You have to add your data, but so far I understand you want something like this?
plot(NULL, xlim=c(2007, 2014), ylim=c(78,86), xlab="Training Years", ylab="Performances")
axis(side=2, at=c(78:86), labels=c(78:86))
x1 <- c(2007:2014)
y1 <- runif(8,78,86)
lines(x1, y1, col="blue")
points(x1,y1, col="blue")
y2 <- runif(8,78,86)
lines(x1, y2, col="red")
points(x1,y2, col="red")
This question already has an answer here:
Ordering of points in R lines plot
(1 answer)
Closed 7 years ago.
I have a simple problem in the plot function of R programming language. I want to draw a line between the points (see this link and how to plot in R), however, what I am getting something weird. I want only one point is connected with another point, so that I can see the function in a continuous fashion, however, in my plot points are connected randomly some other points. Please see the second plot.
Below is the code:
x <- runif(100, -1,1) # inputs: uniformly distributed [-1,1]
noise <- rnorm(length(x), 0, 0.2) # normally distributed noise (mean=0, sd=0.2)
f_x <- 8*x^4 - 10*x^2 + x - 4 # f(x), signal without noise
y <- f_x + noise # signal with noise
# plots
x11()
# plot of noisy data (y)
plot(x, y, xlim=range(x), ylim=range(y), xlab="x", ylab="y",
main = "observed noisy data", pch=16)
x11()
# plot of noiseless data (f_x)
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",
main = "noise-less data",pch=16)
lines(x, f_x, xlim=range(x), ylim=range(f_x), pch=16)
# NOTE: I have also tried this (type="l" is supposed to create lines between the points in the right order), but also not working:
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",
main = "noise-less data", pch=16, type="l")
First plot is correct:
While second is not what I want, I want a continuous plot:
You have to sort the x values:
plot(x, f_x, xlim=range(x), ylim=range(f_x), xlab="x", ylab="y",
main = "noise-less data",pch=16)
lines(x[order(x)], f_x[order(x)], xlim=range(x), ylim=range(f_x), pch=16)
I would like to overlay two plots:
plot1
t1 <- c(0,1,2,3,4,5,6,7,8,9,10)
d1 <- c(0,2,4,6,8,10,12,14,16,18,20)
plot2
t2 <- c(0,1,2,3,4,5)
d2 <- c(1,3,7,8,8,8)
I tried
plot(d1~t1, col="black", type="l")
par(new=T)
plot(d2~t2, col="black", type="l")
But the problem is: in this way, both x axes also overlay each other while x in plot1 is 1:10 and plot2 1:5
You can use lines for the second plot (instead of plot). Furthermore, we scale the x-axis values of the second plot (t2) with 2 (I(2 * t2)).
plot(d1 ~ t1, col="black", type="l", xlim=c(0,10))
lines(d2 ~ I(2 * t2), col="black", type="l", xlim=c(0,5))
In this way, the x-range of the second plot is identical to the x-range of the first one.
I want to plot multiple histograms in R which do not show frequency, but the density instead:
A <- rnorm(100)
B <- rnorm(100)
hist1 <- hist(A,prob=TRUE,breaks=30)
hist2 <- hist(B,prob=TRUE,breaks=30)
plot(hist1, col="red",lty=0, xlim=c(-4,4))
plot(hist2, col="blue", lty=0, xlim=c(-4,4), add=TRUE, main="Example")
lines(density(A))
However, my 'prob=TRUE' option apparently doesn't go through when plotting the objects. Can someone explain to me what I am doing wrong?
leave the prob=T out of the hist() command
hist1 <- hist(A,breaks=30)
hist2 <- hist(B,freq=F,breaks=30)
And put freq=F into the plot command.
plot(hist1, col="red",lty=0, xlim=c(-4,4),freq=F)
plot(hist2, col="blue", lty=0, xlim=c(-4,4), add=TRUE, main="Example",freq=F)
the type argument to xyplot() can take "s" for "steps." From help(plot):
The two step types differ in their x-y preference: Going from
(x1,y1) to (x2,y2) with x1 < x2, 'type = "s"' moves first
horizontal, then vertical, whereas 'type = "S"' moves the other
way around.
i.e. if you use type="s", the horizontal part of the step has its left end attached to the data point, while type="S" has its right end attached to the data point.
library(lattice)
set.seed(12345)
num.points <- 10
my.df <- data.frame(x=sort(sample(1:100, num.points)),
y=sample(1:40, num.points, replace=TRUE))
xyplot(y~x, data=my.df, type=c("p","s"), col="blue", main='type="s"')
xyplot(y~x, data=my.df, type=c("p","S"), col="red", main='type="S"')
How could one achieve a "step" plot, where the vertical motion happens between data points points, i.e. at x1 + (x2-x1)/2, so that the horizontal part of the step is centered on the data point?
Edited to include some example code. better late than never I suppose.
I am using excellent #nico answer to give its lattice version. Even I am ok with #Dwin because the question don't supply a reproducible example, but customizing lattice panel is sometimes challenging.
The idea is to use panel.segments which is the equivalent of segments of base graphics.
library(lattice)
xyplot(y~x,
panel =function(...){
ll <- list(...)
x <- ll$x
y <- ll$y
x.start <- x - (c(0, diff(x)/2))
x.end <- x + (c(diff(x)/2, 0))
panel.segments(x.start, y, x.end, y, col="orange", lwd=2)
panel.segments(x.end[-length(x.end)], y[1:(length(y)-1)],
x.end[-length(x.end)], y[-1], col="orange", lwd=2)
## this is optional just to compare with type s
panel.xyplot(...,type='s')
## and type S
panel.xyplot(...,type='S')
})
This is a base graphics solution, as I am not too much of an expert in lattice.
Essentially you can use segments to draw first the horizontal, then the vertical steps, passing the shifted coordinates as a vector.
Here is an example:
set.seed(12345)
# Generate some data
num.points <- 10
x <- sort(sample(1:100, num.points))
y <- sample(1:40, num.points, replace=T)
# Plot the data with style = "s" and "S"
par(mfrow=c(1,3))
plot(x, y, "s", col="red", lwd=2, las=1,
main="Style: 's'", xlim=c(0, 100))
points(x, y, pch=19, col="red", cex=0.8)
plot(x, y, "S", col="blue", lwd=2, las=1,
main="Style: 'S'", xlim=c(0, 100))
points(x, y, pch=19, col="blue", cex=0.8)
# Now plot our points
plot(x, y, pch=19, col="orange", cex=0.8, las=1,
main="Centered steps", xlim=c(0, 100))
# Calculate the starting and ending points of the
# horizontal segments, by shifting the x coordinates
# by half the difference with the next point
# Note we leave the first and last point as starting and
# ending points
x.start <- x - (c(0, diff(x)/2))
x.end <- x + (c(diff(x)/2, 0))
# Now draw the horizontal segments
segments(x.start, y, x.end, y, col="orange", lwd=2)
# and the vertical ones (no need to draw the last one)
segments(x.end[-length(x.end)], y[1:(length(y)-1)],
x.end[-length(x.end)], y[-1], col="orange", lwd=2)
Here is the result: