我需要这种类型的数据:

我需要x在x轴上,y在y轴上,fx和fy在ploy区域。你能在R上帮我吗,也就是两条曲线相交。我的代码是
gx <- expand.grid(x=seq(1,5,length=50))
fx <- function(x) { exp(-x) }
gx$fx <- apply(gx,1,fx)
plot(gx, type="l",col="red")
gy <- expand.grid(y=seq(1,5,length=50))
fy <- function(y) { y*exp(-y) }
gy$fy <- apply(gy, 1, fy)
par(new=TRUE)
plot(gy, type="l", col="green")

发布于 2017-06-01 14:09:31
不是100%确定我理解这个问题的意思,但是如果你想标记你的轴,你可以使用xlab和ylab图形参数:
即:
plot(gx, type="l",col="red", xlab="label for x axis", ylab="label for y axis")发布于 2017-06-01 14:33:38
下面是base的图:
plot(gx, type="n", xlab="", ylab="")
for(i in 1:2) lines(get(c("gx", "gy")[i]), col=c("red", "green")[i])
title(xlab="x", ylab="y")

我个人更喜欢做一些更多的数据操作,使用“gx”和“gy”包将两个数据(gx和gy)组合成一个长格式的data.frame:
dat <- data.frame(gx, gy)
dat <- dat %>%
gather(xvar, x, x,y) %>%
gather(yvar, y, fx, fy)
head(dat)
# xvar x yvar y
# 1 x 1.000000 fx 0.3678794
# 2 x 1.081633 fx 0.3390415
# 3 x 1.163265 fx 0.3124642
# 4 x 1.244898 fx 0.2879703
# 5 x 1.326531 fx 0.2653964
# 6 x 1.408163 fx 0.2445921这将使使用ggplot可视化变得很容易:
ggplot(dat, aes(x,y, col=yvar)) + geom_line()

发布于 2017-06-01 14:57:35
将legend和lines函数与plot一起使用将产生下图。
plot(gx,type="l",col="red", xlab ="x", ylab="y")
lines(gy,col="green")
legend("topright", inset=.05, cex = 1, c("f(x)","f(y)"), horiz=TRUE, lty=c(1,1), lwd=c(2,2), col=c("red","green"), text.font=3)

https://stackoverflow.com/questions/44299641
复制相似问题