Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中多变量的线图_R_Graphics_Line Plot - Fatal编程技术网

R中多变量的线图

R中多变量的线图,r,graphics,line-plot,R,Graphics,Line Plot,我有以下格式的输入数据 x y z 0 2.2 4.5 5 3.8 6.8 10 4.6 9.3 15 7.6 10.5 如何在R中绘制类似excel(如下所示)的xy散点图 如果你能说出你在这里的想法,可能会有所帮助。在R中,这真的很简单。您应该查阅文档,查找和。对于一个简单的概述,这是很好的。代码如下: windows() plot(x, y, type="l", lwd=2,

我有以下格式的输入数据

  x      y       z
  0      2.2     4.5
  5      3.8     6.8
  10     4.6     9.3
  15     7.6     10.5
如何在R中绘制类似excel(如下所示)的xy散点图


如果你能说出你在这里的想法,可能会有所帮助。在R中,这真的很简单。您应该查阅文档,查找和。对于一个简单的概述,这是很好的。代码如下:

windows()
  plot(x, y, type="l", lwd=2, col="blue", ylim=c(0, 12), xaxs="i", yaxs="i")
  lines(x,z, lwd=2, col="red")
  legend("topleft", legend=c("y","z"), lwd=c(2,2), col=c("blue","red"))
请注意,如果使用Mac,则需要
quartz()
而不是
windows()
。下面是情节:

至少有四种方法可以做到这一点: 1) 在此处使用称为df的“水平”或“宽”data.frame

df <- data.frame(x = c(0, 5, 10, 15), y = c(2.2, 3.8, 4.6, 7.6), z = c(4.5, 6.8, 9.3, 10.5))
ggplot(df, aes(x)) + 
  geom_line(aes(y = y, colour = "y")) + 
  geom_line(aes(y = z, colour = "z"))
3) 将原始df转换为“长”data.frame。在
ggplot2

require("reshape")
require("ggplot2")

mdf <- melt(df, id="x")  # convert to long format
ggplot(mdf, aes(x=x, y=value, colour=variable)) +
    geom_line() + 
    theme_bw()

windows
不是必需的-
plot
无论如何都会调用一个新的图形设备。现在我只在R Studio中工作。如果不添加
windows()
代码,绘图将进入绘图象限,这是我讨厌的&纵横比扭曲的地方b/c我没有为完美的正方形调整大小。在使用R Studio之前,我还使用了
windows()
code,b/c我总是绘制多个绘图&我不希望它们被覆盖。在我看来,这只是一个很好的习惯。很好知道-在RStudio中,
dev.new
是否与
windows
一样工作?它似乎什么都没做。当然,您可以在设备之间移动w/
dev.set()
,例如@gung为什么此图不以0开头?它以小于0的值开始?请检查您的术语。你的标题提到“线图”,但你的问题和标签提到“散点图”。你能说说你有什么问题吗?我认为投票被否决是因为缺少这些信息。另外,如果我知道你到底对什么感到困惑,我可以通过我的回答更具体/更有帮助。
require("reshape")
require("ggplot2")

mdf <- melt(df, id="x")  # convert to long format
ggplot(mdf, aes(x=x, y=value, colour=variable)) +
    geom_line() + 
    theme_bw()
matplot(df$x, df[,2:3], type = "b", pch=19 ,col = 1:2)