2014-01-24 54 views
2

我有一个数据帧df与10列如此,引用x轴可变绘制使用GGPLOT2中的R

row.names c(300, 400, 500, 600, 700) X1 X2 X3 X4 X5 X6 X7 X8 X9 
1 absTLM_3_4 300 -4.147782e-08 -3.360635e-08 -3.306786e-08 -3.133482e-08 -3.124207e-08 -3.056317e-08 -3.020253e-08 -2.950814e-08 -2.955155e-08 
2 absTLM_4_5 400 -3.703708e-08 -3.013687e-08 -2.746570e-08 -2.627163e-08 -2.528328e-08 -2.457543e-08 -2.437666e-08 -2.412295e-08 -2.358447e-08 
3 absTLM_5_6 500 -3.575756e-08 -2.694028e-08 -2.457341e-08 -2.331162e-08 -2.262283e-08 -2.180886e-08 -2.104917e-08 -2.101946e-08 -2.081650e-08 
4 absTLM_6_7 600 -2.454283e-08 -2.152165e-08 -1.967477e-08 -1.885213e-08 -1.835989e-08 -1.814608e-08 -1.806438e-08 -1.785795e-08 -1.784158e-08 
5 absTLM_7_8 700 -2.317125e-10 -1.029456e-08 -1.076342e-08 -1.365264e-08 -1.378578e-08 -1.457421e-08 -1.463740e-08 -1.480280e-08 -1.515121e-0 

现在我想在x绘制第1列轴与纵轴上的2,5,6和8列相对应。这样做我用下面的一段代码,

x4 <- melt(df, id=names(df)[1], measure=names(df)[c(2, 5, 6, 8)], variable = "cols") 
plt <- ggplot(x4) + 
     geom_line(aes(x=x,y= value, color=cols), size=1) + 
     labs(x = "x", y = "y") 

当我打电话剧情对象plt我得到错误的eval(表达式,ENVIR,enclos):对象“X”未找到
有人可以指出我应该改变什么来正确显示图。

谢谢。

+3

有没有名为'x'在'X4'列。参见'head(x4)'并相应地调整你的'geom_line(aes())'。 –

+1

它看起来像列被称为'X1',而不是'x'。 –

+0

@BrandonBertelsen看来,我需要被命名为'C(300,400,500,600,700)的第一列的列标题',现在当我用'PLT < - ggplot(×4)+ geom_line(AES(X = c(300,400,500,600,700),y = value,color = cols),size = 1)+ labs(x =“x”,y =“y”)'我仍然收到错误因为,美学必须是长度为1或与dataProblems的长度相同:c(300,400,500,600,700) – Amm

回答

1

它看起来像你的列名是buggered。试试这个:

x4 <- melt(df, id=names(df)[1], measure=names(df)[c(2, 5, 6, 8)], variable = "cols") 
colnames(x4)[1] <- "x" 
plt <- ggplot(x4) + 
     geom_line(aes(x=x,y= value, color=cols), size=1) + 
     labs(x = "x", y = "y") 
+0

是的,你是对的。更改列标题名称后,它可以正常工作!感谢您的有用建议。 – Amm

0

为什么下面这段代码没有正确绘制数据的原因是因为这是在数据帧df第一列的名称列标题名称c(300, 400, 500, 600, 700)

x4 <- melt(df, id=names(df)[1], measure=names(df)[c(2, 5, 6, 8)], variable = "cols") 
plt <- ggplot(x4) + 
     geom_line(aes(x=x,y= value, color=cols), size=1) + 
     labs(x = "x", y = "y") 

所以的df列标题名称必须首先改变,根据由布兰登贝特尔森的建议,的c(300, 400, 500, 600, 700)列标题名称是由下面的行的代码变更为x

colnames(df)[1] <- "x" 

一旦列标题名称被改变时,则该数据帧可被熔化之后,绘图工作正确,

x4 <- melt(df, id=names(df)[1], measure=names(df)[c(2, 5, 6, 8)], variable = "cols") 
plt <- ggplot(x4) + 
     geom_point(aes(x=x,y= value, color=cols), size=2) + 
     labs(x = "x", y = "y") 

所以剧情plt看起来像这样,

enter image description here