在同一窗口中绘制一个或多个图

发布于 2024-10-08 02:58:43 字数 59 浏览 0 评论 0原文

我想比较两条曲线,是否可以使用 R 绘制一个图,然后在其上绘制另一个图?如何 ?

谢谢。

I want compare two curves, it's possible with R to draw a plot and then draw another plot over it ? how ?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜无邪 2024-10-15 02:58:43

使用基本 R,您可以绘制一条曲线,然后使用 lines() 参数添加第二条曲线。这是一个简单的示例:

x <- 1:10
y <- x^2
y2 <- x^3

plot(x,y, type = "l")
lines(x, y2, col = "red")

或者,如果您想使用 ggplot2,这里有两种方法 - 一种在同一个图上绘制不同的颜色,另一种为每个变量生成单独的图。这里的技巧是首先将数据“融化”为长格式。

library(ggplot2)

df <- data.frame(x, y, y2)

df.m <- melt(df, id.var = "x")

qplot(x, value, data = df.m, colour = variable, geom = "line")

qplot(x, value, data = df.m, geom = "line")+ facet_wrap(~ variable)

With base R, you can plot your one curve and then add the second curve with the lines() argument. Here's a quick example:

x <- 1:10
y <- x^2
y2 <- x^3

plot(x,y, type = "l")
lines(x, y2, col = "red")

Alternatively, if you wanted to use ggplot2, here are two methods - one plots different colors on the same plot, and the other generates separate plots for each variable. The trick here is to "melt" the data into long format first.

library(ggplot2)

df <- data.frame(x, y, y2)

df.m <- melt(df, id.var = "x")

qplot(x, value, data = df.m, colour = variable, geom = "line")

qplot(x, value, data = df.m, geom = "line")+ facet_wrap(~ variable)
2024-10-15 02:58:43

使用 lattice 包

require(lattice)
x <- seq(-3,3,length.out=101)
xyplot(dnorm(x) + sin(x) + cos(x) ~ x, type = "l") 

格子曲线图

Using lattice package:

require(lattice)
x <- seq(-3,3,length.out=101)
xyplot(dnorm(x) + sin(x) + cos(x) ~ x, type = "l") 

Lattice curve plot

小耗子 2024-10-15 02:58:43

我们已经为您提供了一些解决方案。如果您继续使用基础包,您应该熟悉以下函数:plot()、lines()、abline()、points()、polygon()、segment()、rect()、box()、 arrows(), ...看看他们的帮助文件。

您应该看到基础包中的绘图作为一个窗格,其中包含您给它的坐标。在该窗格中,您可以使用上述功能绘制一整套对象。它们允许您根据需要构建图表。不过你应该记住,除非你像 Dr. G 那样使用标准设置,否则每次调用plot()都会给你一个新的窗格。还要考虑到事物可以在其他事物之上绘制,因此请考虑用于绘制事物的顺序。

参见例如:

set.seed(100)
x <- 1:10
y <- x^2
y2 <- x^3
yse <- abs(runif(10,2,4))

plot(x,y, type = "n")  # type="n" only plots the pane, no curves or points.

# plots the area between both curves
polygon(c(x,sort(x,decreasing=T)),c(y,sort(y2,decreasing=T)),col="grey")
# plot both curves
lines(x,y,col="purple")
lines(x, y2, col = "red")
# add the points to the first curve
points(x, y, col = "black")
# adds some lines indicating the standard error
segments(x,y,x,y+yse,col="blue")
# adds some flags indicating the standard error
arrows(x,y,x,y-yse,angle=90,length=0.1,col="darkgreen")

这给你:

alt text

There's been some solutions already for you. If you stay with the base package, you should get acquainted with the functions plot(), lines(), abline(), points(), polygon(), segments(), rect(), box(), arrows(), ...Take a look at their help files.

You should see a plot from the base package as a pane with the coordinates you gave it. On that pane, you can draw a whole set of objects with the abovementioned functions. They allow you to construct a graph as you want. You should remember though that, unless you play with the par settings like Dr. G showed, every call to plot() gives you a new pane. Also take into account that things can be plot over other things, so think about the order you use to plot things.

See eg:

set.seed(100)
x <- 1:10
y <- x^2
y2 <- x^3
yse <- abs(runif(10,2,4))

plot(x,y, type = "n")  # type="n" only plots the pane, no curves or points.

# plots the area between both curves
polygon(c(x,sort(x,decreasing=T)),c(y,sort(y2,decreasing=T)),col="grey")
# plot both curves
lines(x,y,col="purple")
lines(x, y2, col = "red")
# add the points to the first curve
points(x, y, col = "black")
# adds some lines indicating the standard error
segments(x,y,x,y+yse,col="blue")
# adds some flags indicating the standard error
arrows(x,y,x,y-yse,angle=90,length=0.1,col="darkgreen")

This gives you :

alt text

冬天旳寂寞 2024-10-15 02:58:43

看看标准杆

> ?par
> plot(rnorm(100))
> par(new=T)
> plot(rnorm(100), col="red")

Have a look at par

> ?par
> plot(rnorm(100))
> par(new=T)
> plot(rnorm(100), col="red")
夜空下最亮的亮点 2024-10-15 02:58:43

ggplot2 对于这类事情来说是一个很棒的包:

install.packages('ggplot2')
require(ggplot2)
x <- 1:10
y1 <- x^2
y2 <- x^3
df <- data.frame(x = x, curve1 = y1, curve2 = y2)
df.m <- melt(df, id.vars = 'x', variable_name = 'curve' )
# now df.m is a data frame with columns 'x', 'curve', 'value'
ggplot(df.m, aes(x,value)) + geom_line(aes(colour = curve)) + 
geom_point(aes(shape=curve))

你可以得到按曲线着色的图,每条曲线都有不同的点标记,还有一个漂亮的图例,所有这些都轻松无痛,无需任何额外的工作:

替代文字

ggplot2 is a great package for this sort of thing:

install.packages('ggplot2')
require(ggplot2)
x <- 1:10
y1 <- x^2
y2 <- x^3
df <- data.frame(x = x, curve1 = y1, curve2 = y2)
df.m <- melt(df, id.vars = 'x', variable_name = 'curve' )
# now df.m is a data frame with columns 'x', 'curve', 'value'
ggplot(df.m, aes(x,value)) + geom_line(aes(colour = curve)) + 
geom_point(aes(shape=curve))

You get the plot coloured by curve, and with different piont marks for each curve, and a nice legend, all painlessly without any additional work:

alt text

撩人痒 2024-10-15 02:58:43

使用 matplot 函数同时绘制多条曲线。请帮助(matplot)了解更多信息。

Draw multiple curves at the same time with the matplot function. Do help(matplot) for more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文