提问人:Sandra Schlichting 提问时间:4/2/2010 最后编辑:M--Sandra Schlichting 更新时间:2/24/2023 访问量:1830257
在同一图中绘制两个图形
Plot two graphs in a same plot
问:
我想在同一图中绘制 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中可以做到,但是有谁知道如何在R中做到这一点?hold on
答:
lines()
或者将添加到现有图形中,但不会创建新窗口。所以你需要做points()
plot(x,y1,type="l",col="red")
lines(x,y2,col="green")
评论
plot(sin); curve(cos, add=TRUE)
如果您使用的是基础图形(即不是格子/网格图形),则可以通过使用点/线/多边形函数来模拟 MATLAB 的保持功能,从而在不开始新绘图的情况下向绘图添加其他细节。在多图布局的情况下,您可以使用它来选择要添加内容的图。par(mfg=...)
您还可以在同一图形上使用和绘制不同的轴。内容如下:par
plot( x, y1, type="l", col="red" )
par(new=TRUE)
plot( x, y2, type="l", col="green" )
如果您详细阅读 ,您将能够生成非常有趣的图表。另一本值得一看的书是 Paul Murrel 的 R Graphics。par
R
评论
xlab="", ylab="", ...
plot
与其将要绘制的值保留在数组中,不如将它们存储在矩阵中。默认情况下,整个矩阵将被视为一个数据集。但是,如果在图中添加相同数量的修饰符,例如 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")
除非数据集的大小不同,否则这应该有效。
评论
也就是说,您可以将点用于过图。
plot(x1, y1,col='red')
points(x2,y2,col='blue')
在构建多层图时,应考虑包。这个想法是创建一个具有基本美学的图形对象,并逐步增强它。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
生成绘图,您可以在每个阶段看到它(好吧,在创建至少一个图层之后)。剧情的进一步附魔也是用创建的对象制作的。例如,我们可以为 axises 添加标签:
g <- g + ylab("Y") + xlab("X")
g
最终的样子是:g
更新 (2013-11-08):
正如评论中指出的,的理念建议使用长格式的数据。
您可以参考此答案,以便查看相应的代码。ggplot
评论
ggplot
如@redmode所述,您可以使用 在同一图形设备中绘制两条线。在该答案中,数据采用“宽”格式。但是,在使用时,通常最方便的做法是将数据以“长”格式保存在数据框中。然后,通过在理论参数中使用不同的“分组变量”,线条的属性(例如线型或颜色)将根据分组变量而变化,并出现相应的图例。ggplot
ggplot
aes
在这种情况下,我们可以使用美学,它将线条的颜色与数据集中变量的不同级别相匹配(此处:y1 与 y2)。但首先,我们需要将数据从宽格式融化为长格式,例如使用包中的“melt”函数。下面介绍了重塑数据的其他方法:将 data.frame 从宽格式调整为长格式。colour
reshape2
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()
您可以使用 plotly 包中的函数将此处的任何 gggplot2 示例转换为交互式绘图,但我认为这种绘图在没有 ggplot2 的情况下会更好:ggplotly()
# 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")
评论
我认为你要找的答案是:
plot(first thing to plot)
plot(second thing to plot,add=TRUE)
评论
"add" is not a graphical parameter
"add" is not a graphical parameter
R version 3.2.3 (2015-12-10)
par(new=TRUE)
使用以下函数:matplot
matplot(x, cbind(y1,y2),type="l",col=c("red","green"),lty=c(1,1))
如果 和 在相同的点上进行评估,请使用此选项。它缩放 Y 轴以适合更大(或)的答案,这与这里的其他一些答案不同,如果它变得大于(ggplot 解决方案大多可以接受),它们就会被剪裁。y1
y2
x
y1
y2
y2
y1
或者,如果两条线的 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
评论
如果要将绘图拆分为两列(2 个图彼此相邻),可以这样做:
par(mfrow=c(1,2))
plot(x)
plot(y)
tl;dr:你想用(with)或。curve
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
将绘制一个函数,如 . 使用 x 和 y 值绘制点,例如:。curve(sin)
lines
lines( x=0:10, y=sin(0:10) )
这里有一个细微的区别:需要调用你正在尝试做的事情,同时已经假设你正在添加到现有情节中。curve
add=TRUE
lines
这是调用 plot(0:2); curve(sin)
的结果。
在幕后,请查看 .并检查.当你调用 R 时,它会发现这是一个函数(不是 y 值)并使用该方法,该方法最终调用 .用于处理函数的工具也是如此。methods(plot)
body( plot.function )[[5]]
plot(sin)
sin
plot.function
curve
curve
您还可以使用 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')
这将创建以下图:
惯用的Matlab可以在R中翻译,例如:plot(x1,y1,x2,y2)
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。
我们也可以使用格子库
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"))))
使用(从主 y 轴和次 y 轴添加解决方案 - 它似乎丢失了):plotly
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))
工作演示截图:
评论
Error in library(plotly) : there is no package called ‘plotly’
plotly
install.packages("plotly")
用于数学函数。
并使用 add=TRUE 使用相同的绘图和轴。curve
curve( log2 , to=5 , col="black", ylab="log's(.)")
curve( log , add=TRUE , col="red" )
curve( log10, add=TRUE , col="blue" )
abline( h=0 )
评论
?curve
add=TRUE