2014-01-24 62 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