2014-01-21 80 views
0

我有一个矩阵:绘制矩阵的第二列和第三列,并使用第一列作为x轴的值使用ggplot()

a<-c(0.863950263765782, 0.908204672069546, 0.931564826815488, 0.947460752561418, 0.959463328487368, 0.969060077042643, 0.977026409365163, 0.983818665594281,0.989719993426693) 

b<-c(0.787029935566084, 0.872407618393809, 0.925762651962245, 0.948331740835959, 0.961947304263652, 0.971489926009247, 0.978878398154957, 0.984799730999978, 0.9899910908) 

c<-c("0.1-0.9", "0.2-0.9", "0.3-0.9", "0.4-0.9", "0.5-0.9", "0.6-0.9", "0.7-0.9", "0.8-0.9", "0.9") 

e<-cbind(c,a,b) 

我想在同一个图形和利用绘制e[,2]e[,3]e[,1]中的对应条目作为x轴的值。

如何使用ggplotplot()来完成?

  1. 我试过melt(e, id.vars=1:1)但我无法获得正确格式的变量。

回答

1
require(reshape2) 
require(ggplot2) 
e <- melt(as.data.frame(e), id.vars="c", measure.vars=c("a", "b"), variable.name="var", value.name="val") 
ggplot(e, aes(x=c, y=val, colour=var)) + geom_point() 

编辑:

奇怪。此代码...

a<-c("0.863950263765782", "0.908204672069546", "0.931564826815488", "0.947460752561418", "0.959463328487368", "0.969060077042643", "0.977026409365163", "0.983818665594281","0.989719993426693") 
b<-c("0.787029935566084", "0.872407618393809", "0.925762651962245", "0.948331740835959", "0.961947304263652", "0.971489926009247", "0.978878398154957", "0.984799730999978", "0.9899910908") 
c<-c("0.1-0.9", "0.2-0.9", "0.3-0.9", "0.4-0.9", "0.5-0.9", "0.6-0.9", "0.7-0.9", "0.8-0.9", "0.9") 
e<-cbind(c,a,b) 
require(reshape2) 
require(ggplot2) 
e <- melt(as.data.frame(e), id.vars="c", measure.vars=c("a", "b"), variable.name="var", value.name="val") 
ggplot(e, aes(x=c, y=val, colour=var)) + 
    geom_point() 

......导致我的机器上输出:

enter image description here

ggplot(e, aes(x=c, y=as.numeric(val), colour=var, group=var)) + geom_point() + geom_line() 

...导致这个:

enter image description here

+0

不知道如何为类型函数的对象自动选择比例。默认为连续 eval(expr,envir,enclos)中的错误:未找到对象'val' > – user1723765

+0

我将val更改为value并将var更改为变量,但仍然没有真正执行它应该的操作 – user1723765

+0

@ user1723765绘制y上的数字和x上的标签 - 我猜 - 应该这样做,在这里。 – lukeA

相关问题