我想在同一个图中绘制 y1 和 y2 。
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
plot(x, y1, type = "l", col = "red")
plot(x, y2, type = "l", col = "green")
但是当我这样做时,它们并没有一起绘制在同一个情节中。
在 Matlab 中可以做到hold on
,但有人知道如何在 R 中做到这一点吗?
lines()
或points()
将添加到现有图形,但不会创建新窗口。所以你需要做
plot(x,y1,type="l",col="red")
lines(x,y2,col="green")
您还可以par
在同一图表但不同的轴上使用和绘制。如下所示:
plot( x, y1, type="l", col="red" )
par(new=TRUE)
plot( x, y2, type="l", col="green" )
如果您详细阅读 in par
,R
您将能够生成非常有趣的图表。另一本书是 Paul Murrel 的 R Graphics。
在构建多层地块时,应考虑ggplot
包装。这个想法是创建一个具有基本美学的图形对象并逐步增强它。
ggplot
style 需要将数据打包在data.frame
.
# Data generation
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
df <- data.frame(x,y1,y2)
基本解决方案:
require(ggplot2)
ggplot(df, aes(x)) + # basic graphical object
geom_line(aes(y=y1), colour="red") + # first layer
geom_line(aes(y=y2), colour="green") # second layer
这里+ operator
用于向基本对象添加额外的层。
您可以在ggplot
绘图的每个阶段访问图形对象。比如说,通常的分步设置可能如下所示:
g <- ggplot(df, aes(x))
g <- g + geom_line(aes(y=y1), colour="red")
g <- g + geom_line(aes(y=y2), colour="green")
g
g
生成情节,您可以在每个阶段看到它(好吧,在创建至少一个图层之后)。情节的进一步魅力也由创建的对象制成。例如,我们可以为轴添加标签:
g <- g + ylab("Y") + xlab("X")
g
最终g
看起来像:
更新(2013-11-08):
正如评论中所指出的,ggplot
的理念建议使用长格式的数据。您可以参考此答案以查看相应的代码。
我认为您正在寻找的答案是:
plot(first thing to plot)
plot(second thing to plot,add=TRUE)
使用matplot
功能:
matplot(x, cbind(y1,y2),type="l",col=c("red","green"),lty=c(1,1))
如果y1
和y2
在同x
一点进行评估,请使用此选项。它缩放 Y 轴以适应更大的(y1
或y2
),这与此处的其他一些答案不同,y2
如果它大于y1
(ggplot 解决方案对此大多可以接受),则会剪辑。
或者,如果两条线没有相同的 x 坐标,则在第一个图上设置轴限制并添加:
x1 <- seq(-2, 2, 0.05)
x2 <- seq(-3, 3, 0.05)
y1 <- pnorm(x1)
y2 <- pnorm(x2,1,1)
plot(x1,y1,ylim=range(c(y1,y2)),xlim=range(c(x1,x2)), type="l",col="red")
lines(x2,y2,col="green")
我很惊讶这个 Q 已经 4 岁了,没有人提到matplot
或x/ylim
......
tl; dr:您想使用curve
(with add=TRUE
) 或lines
.
我不同意,par(new=TRUE)
因为这会重复打印刻度线和轴标签。例如
的输出plot(sin); par(new=T); plot( function(x) x**2 )
。
看看垂直轴标签是多么混乱!由于范围不同,您需要设置ylim=c(lowest point between the two functions, highest point between the two functions)
,这比我将要向您展示的要容易 - 如果您不仅要添加两条曲线,而且要添加许多曲线,则更不容易。
curve
总是让我对绘图感到困惑的是和之间的区别lines
。(如果你不记得这些是两个重要的绘图命令的名称,那就唱吧。)
curve
这是和之间的最大区别lines
。curve
将绘制一个函数,例如curve(sin)
. lines
绘制具有 x 和 y 值的点,例如:lines( x=0:10, y=sin(0:10) )
.
这里有一个小的区别:curve
需要add=TRUE
为你想要做的事情调用,同时lines
已经假设你正在添加到现有的情节中。
这是调用的结果plot(0:2); curve(sin)
。
在幕后,检查methods(plot)
。并检查body( plot.function )[[5]]
。当您调用plot(sin)
R 时发现这sin
是一个函数(不是 y 值)并使用该plot.function
方法,最终调用curve
. curve
用于处理功能的工具也是如此。
正如@redmode 所述,您可以使用ggplot
. 在那个答案中,数据采用“宽”格式。但是,在使用ggplot
时,通常最方便的是以“长”格式将数据保存在数据框中。然后,通过在aes
thetics 参数中使用不同的“分组变量”,线条的属性,例如线型或颜色,将根据分组变量而变化,并出现相应的图例。
在这种情况下,我们可以使用colour
美学,它将线条的颜色与数据集中变量的不同级别(这里:y1 vs y2)相匹配。但首先我们需要将数据从宽格式转换为长格式,例如使用 package.json 中的函数“melt” reshape2
。此处描述了重塑数据的其他方法:Reshaping data.frame from wide to long format。
library(ggplot2)
library(reshape2)
# original data in a 'wide' format
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
df <- data.frame(x, y1, y2)
# melt the data to a long format
df2 <- melt(data = df, id.vars = "x")
# plot, using the aesthetics argument 'colour'
ggplot(data = df2, aes(x = x, y = value, colour = variable)) + geom_line()
如果您使用的是基本图形(即不是晶格/网格图形),那么您可以通过使用点/线/多边形函数来模拟 MATLAB 的保持功能,从而在不开始新的绘图的情况下为绘图添加额外的细节。在多图布局的情况下,您可以使用par(mfg=...)
来选择要添加内容的图。
您可以将点用于重叠图,也就是说。
plot(x1, y1,col='red')
points(x2,y2,col='blue')
与其将要绘制的值保存在数组中,不如将它们存储在矩阵中。默认情况下,整个矩阵将被视为一个数据集。但是,如果您在图中添加相同数量的修饰符,例如 col(),因为您在矩阵中有行,R 将计算出应该独立处理每一行。例如:
x = matrix( c(21,50,80,41), nrow=2 )
y = matrix( c(1,2,1,2), nrow=2 )
plot(x, y, col("red","blue")
除非您的数据集大小不同,否则这应该有效。
惯用的 Matlabplot(x1,y1,x2,y2)
可以用 R 语言翻译,ggplot2
例如:
x1 <- seq(1,10,.2)
df1 <- data.frame(x=x1,y=log(x1),type="Log")
x2 <- seq(1,10)
df2 <- data.frame(x=x2,y=cumsum(1/x2),type="Harmonic")
df <- rbind(df1,df2)
library(ggplot2)
ggplot(df)+geom_line(aes(x,y,colour=type))
灵感来自赵婷婷的具有不同 x 轴范围的双线图使用 ggplot2。
您可以使用plotly包中的ggplotly()
函数将此处的任何gggplot2示例转换为交互式图,但我认为如果没有ggplot2 ,这种图会更好:
# call Plotly and enter username and key
library(plotly)
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
plot_ly(x = x) %>%
add_lines(y = y1, color = I("red"), name = "Red") %>%
add_lines(y = y2, color = I("green"), name = "Green")
您还可以使用ggvis创建绘图:
library(ggvis)
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
df <- data.frame(x, y1, y2)
df %>%
ggvis(~x, ~y1, stroke := 'red') %>%
layer_paths() %>%
layer_paths(data = df, x = ~x, y = ~y2, stroke := 'blue')
这将创建以下图:
使用plotly
(从主要和次要 y 轴添加解决方案plotly
- 似乎丢失了):
library(plotly)
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
df=cbind.data.frame(x,y1,y2)
plot_ly(df) %>%
add_trace(x=~x,y=~y1,name = 'Line 1',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE) %>%
add_trace(x=~x,y=~y2,name = 'Line 2',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE,yaxis = "y2") %>%
layout(title = 'Title',
xaxis = list(title = "X-axis title"),
yaxis2 = list(side = 'right', overlaying = "y", title = 'secondary y axis', showgrid = FALSE, zeroline = FALSE))
工作演示的屏幕截图:
我们也可以使用 lattice 库
library(lattice)
x <- seq(-2,2,0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
xyplot(y1 + y2 ~ x, ylab = "y1 and y2", type = "l", auto.key = list(points = FALSE,lines = TRUE))
对于特定颜色
xyplot(y1 + y2 ~ x,ylab = "y1 and y2", type = "l", auto.key = list(points = F,lines = T), par.settings = list(superpose.line = list(col = c("red","green"))))