I'm trying to get some points
on my density plot for the seq(-3, 3)
[ie, 7 numbers]. I get the 7 corresponding density values but when I try to execute points
, I get:
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
As really there is no length difference, I assume there is class()
difference problem between x, and y for points()
. I appreciate a solution?
Here is the R code:
positions = rnorm(1e4)
DENS = density(positions, adjust = 2, n = 1e4)
x.DENS = DENS$x
y.DENS = DENS$y
plot( DENS, col = "red", lwd = 3, xlab = "Positions",
ylab = "Density", xlim = c(-6, 6), main =
NA, bty = 'n', zero.line = F)
x.DENS.2 = seq(-3, 3)
y.DENS.2 = approx(x.DENS, y.DENS, xout = x.DENS.2 ) ## get the x.DENS.2 density values
points(x.DENS.2, y.DENS.2) ## Error
The last line is incorrect. Please change it to
points(x.DENS.2, y.DENS.2$y)
Here is the full codes. It works on my side. So, when you plot the results, it will be very helpful to check the dimension of your inputs to make sure that they matched.
positions = rnorm(1e4)
DENS = density(positions, adjust = 2, n = 1e4)
x.DENS = DENS$x
y.DENS = DENS$y
plot( DENS, col = "red", lwd = 3, xlab = "Positions",
ylab = "Density", xlim = c(-6, 6), main =
NA, bty = 'n', zero.line = F)
x.DENS.2 = seq(-3, 3)
y.DENS.2 = approx(x.DENS, y.DENS, xout = x.DENS.2 ) ## get the x.DENS.2 density values
points(x.DENS.2, y.DENS.2$y)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.