2

I have a data frame df with 10 columns such as this,

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

Now I would like to plot column 1 in the x-axis versus columns 2,5,6, and 8 in the y-axis. For doing this I use the following piece of code,

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")

When I call the plot object plt I get an Error in eval(expr, envir, enclos) : object 'x' not found.
Could someone kindly point out what I should change here to display the plot correctly.

Thanks.

Amm
  • 1,749
  • 4
  • 17
  • 27
  • 3
    There's no column named `x` in `X4`. See `head(x4)` and adjust your `geom_line(aes())` accordingly. – Brandon Bertelsen Jan 24 '14 at 11:21
  • 1
    It looks like the column is called `X1`, not `x`. – Richie Cotton Jan 24 '14 at 11:22
  • @BrandonBertelsen it seems column header of the first column which I need is named `c(300, 400, 500, 600, 700)` , now when I use `plt <- ggplot(x4) + geom_line(aes(x=c(300, 400, 500, 600, 700),y= value, color=cols), size=1) + labs(x = "x", y = "y")` I still get an error such as, Aesthetics must either be length one, or the same length as the dataProblems:c(300, 400, 500, 600, 700) – Amm Jan 24 '14 at 12:32
  • 1
    I think you should just rename the columns and use a typical style (no space in column names only `_` or `.`). `colnames(df)[1] <- "x"` and perhaps your original code will work. Could you provide us with `dput(head(df))`? – Brandon Bertelsen Jan 24 '14 at 13:01
  • 1
    [yep, again, consider renaming your columns](http://stackoverflow.com/questions/21308879/plotting-multiple-data-in-a-data-frame-at-fixed-column-intervals-with-correspond#comment32117134_21308879) – baptiste Jan 24 '14 at 13:29

2 Answers2

1

It looks like your column names are buggered. Try this:

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")
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • Yes, you were right. After changing the column header name it works! Thanks for your useful suggestions. – Amm Jan 24 '14 at 13:14
0

The reason why the following piece of code did not correctly plot the data was due to the column header name c(300, 400, 500, 600, 700) which was name of the first column in the data frame df.

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")

So the column header name of df had to be changed first, according to the suggestion by Brandon Bertelsen, the column header name of c(300, 400, 500, 600, 700) was changed to x by the following line of code,

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

Once the column header name is changed, then the data frame can be melted after which the plotting works correctly,

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")

So the plot plt looks like this,

enter image description here

Amm
  • 1,749
  • 4
  • 17
  • 27