【发布时间】:2010-12-24 21:58:07
【问题描述】:
我想要绘制多组 xy 对。我希望每组 xy 对通过一条线连接。换句话说,我们的目标是拥有多个实验实例,每个实例都由绘制在一个图上的一条线来近似。另外,我将如何为线条着色?
plot 函数可以满足我的要求,但需要一组 xy 对:
plot(x, y, ...)
这个函数可以做多组还是有另一个函数呢?
【问题讨论】:
标签: r statistics plot
我想要绘制多组 xy 对。我希望每组 xy 对通过一条线连接。换句话说,我们的目标是拥有多个实验实例,每个实例都由绘制在一个图上的一条线来近似。另外,我将如何为线条着色?
plot 函数可以满足我的要求,但需要一组 xy 对:
plot(x, y, ...)
这个函数可以做多组还是有另一个函数呢?
【问题讨论】:
标签: r statistics plot
要使用普通的绘图命令执行此操作,我通常会创建一个绘图,然后使用 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)
或者geom_line()在ggplot2中:
library(ggplot2)
ggplot(melt(df, id.vars="x"), aes(x, value, colour = variable,
group = variable)) + geom_line() + theme_bw()
这是另一个例子,包括每对的点(来自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)")
同样的情节使用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")
【讨论】: