在 R 中绘制多组点

发布于 2024-08-13 14:41:58 字数 187 浏览 4 评论 0原文

我有多组要绘制的 xy 对。我希望每组 xy 对都通过一条线连接。换句话说,目标是拥有多个实验实例,每个实例都用在一个图上绘制的线来近似。另外我如何给线条涂上不同的颜色?

绘图函数执行我想要的操作,但采用一组 xy 对: plot(x, y, ...)

这个函数可以接受多个集合吗?或者还有其他函数吗?

I have multiple sets of xy pairs that I want to plot. I want each set of xy pairs to be connected by a line. In other words the goal is to have multiple experimental instances each approximated by a line plotted on one plot. Also how would I colour the lines differently?

The plot function does what I want, but takes on one set of xy pairs:
plot(x, y, ...)

Can this function be made to take multiple sets or is there another function for that?

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

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

发布评论

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

评论(1

烟火散人牵绊 2024-08-20 14:41:58

要使用普通绘图命令执行此操作,我通常会创建一个绘图,然后使用 lines() 函数添加更多行。

否则你可以使用lattice或ggplot2。这里有一些数据:

df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)

您可以使用lattice中的xyplot()

library(lattice)
xyplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)

或ggplot2中的geom_line()

library(ggplot2)
ggplot(melt(df, id.vars="x"), aes(x, value, colour = variable,
        group = variable)) + geom_line() + theme_bw()

这是另一个例子,包括每对的点(来自这篇文章在学习者博客上):

library(lattice)
dotplot(VADeaths, type = "o", auto.key = list(lines = TRUE,
     space = "right"), main = "Death Rates in Virginia - 1940",
     xlab = "Rate (per 1000)")

使用 ggplot2 绘制相同的图:

library(ggplot2)
p <- ggplot(melt(VADeaths), aes(value, X1, colour = X2,
             group = X2))
p + geom_point() + geom_line() + xlab("Rate (per 1000)") +
         ylab("") + opts(title = "Death Rates in Virginia - 1940")

To do this with the normal plot command, I would usually create one plot and then add more lines using the lines() function.

Otherwise you can use lattice or ggplot2. Here's some data:

df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)

You can use xyplot() from lattice:

library(lattice)
xyplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)

Or geom_line() in ggplot2:

library(ggplot2)
ggplot(melt(df, id.vars="x"), aes(x, value, colour = variable,
        group = variable)) + geom_line() + theme_bw()

Here's another example including points at each pair (from this post on the learnr blog):

library(lattice)
dotplot(VADeaths, type = "o", auto.key = list(lines = TRUE,
     space = "right"), main = "Death Rates in Virginia - 1940",
     xlab = "Rate (per 1000)")

And the same plot using ggplot2:

library(ggplot2)
p <- ggplot(melt(VADeaths), aes(value, X1, colour = X2,
             group = X2))
p + geom_point() + geom_line() + xlab("Rate (per 1000)") +
         ylab("") + opts(title = "Death Rates in Virginia - 1940")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文