1

我有一个如下所示的数据文件:

x   ys --------------------->
1   20   25   30   12   22   12
2   12    9   12   32   12 
3   33   12   11    6    1
4    5   10   41   12    3
5    7   81   12   31    8   3   4   11

我想制作一个带有一个 x 值和多个 y 值(ys)的散点图。我试图将 reshape 与熔化一起使用,但我无法创建正确的数据结构来制作此图。如何在 R 中执行此操作并使用 ggplot 绘图?谢谢您的帮助。

4

2 回答 2

2

那么什么不起作用melt呢?你有什么问题geom_point()?很难说这是否是您想要的:

library( "reshape2" )
library( "ggplot2" )

df <- data.frame( x = rnorm(20), ya = rnorm(20), yb = rnorm(20), yc = rnorm(20) )
df <- melt(df, id.vars="x", variable.name="class", value.name="y")

ggplot( df, aes( x = x, y = y) ) +
  geom_point( aes(colour = class) )

ggplot( df, aes( x = x, y = y) ) +
  geom_point() +
  facet_wrap( "class" )
于 2012-11-08T18:59:57.823 回答
0

您可以使用该matplot功能。

假设您的数据在一个名为的对象myDat中,并且x值在列中1并且y值在其他列中,

matplot(x = myDat[, 1], y = myDat[, -1], type = "p", pch = 21)

会产生类似的东西

在此处输入图像描述

或使用主题latticeExtraggplot2like

library(latticeExtra)

xyplot(as.formula(paste(paste0(names(myDat)[-1], collapse = "+"), "~",
  names(myDat[1]))),
  data = myDat, par.settings = ggplot2like(), grid = TRUE)

在此处输入图像描述

于 2012-11-08T21:21:51.410 回答