Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top