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?
我想要绘制多组xy对。我希望每组xy对都通过一条线连接起来。换句话说,目标是具有多个实验实例,每个实验实例通过在一个图上绘制的线来近似。另外,我将如何对线条进行不同的着色?
The plot function does what I want, but takes on one set of xy pairs: plot(x, y, ...)
绘图函数做我想要的,但是接受一组xy对:plot(x,y,...)
Can this function be made to take multiple sets or is there another function for that?
可以将此功能设置为多个集合还是有其他功能?
1 个解决方案
#1
9
To do this with the normal plot command, I would usually create one plot and then add more lines using the lines()
function.
要使用普通的plot命令执行此操作,我通常会创建一个绘图,然后使用lines()函数添加更多行。
Otherwise you can use lattice or ggplot2. Here's some data:
否则你可以使用lattice或ggplot2。这是一些数据:
df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)
You can use xyplot()
from lattice:
你可以使用格子中的xyplot():
library(lattice)
xyplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)
Or geom_line()
in ggplot2:
或ggplot2中的geom_line():
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:
使用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")
#1
9
To do this with the normal plot command, I would usually create one plot and then add more lines using the lines()
function.
要使用普通的plot命令执行此操作,我通常会创建一个绘图,然后使用lines()函数添加更多行。
Otherwise you can use lattice or ggplot2. Here's some data:
否则你可以使用lattice或ggplot2。这是一些数据:
df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)
You can use xyplot()
from lattice:
你可以使用格子中的xyplot():
library(lattice)
xyplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)
Or geom_line()
in ggplot2:
或ggplot2中的geom_line():
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:
使用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")