Daniel
Daniel

Reputation: 289

Negative values not accepted as numeric for X-axis in R:ggplot

This is likely something simple, but I can't work it out.

I am loading a dataset looking like this, but which runs from -50 through to 1500:

Name    ExpA    ExpB    -50     -49     -48     -47     -46    -45      -44
sampleA Light   Low     12.326  12.326  12.328  36.979  36.979  24.566  24.652
SampleB Light   High    0.0     9.333   9.3233  37.302  46.628  18.651  18.651
SampleC Dark    Low     0.0     0.0     0.0     13.575  40.725  27.150  0.0

I perform a melt on the data:

x.melt <-melt(x, id=c("Name","ExpA", "ExpB"))

x.melt
    Name    ExpA    ExpB    variable    value
1   SampleA Light   Low     -50     12.326402
2   SampleB Light   High    -50     0.000000
3   SampleC Dark    Low     -50     14.242440
4   SampleD Dark    High    -50     0.000000
9   SampleA Light   Low     -49     12.326402
10  SampleB Light   High    -49     9.325700
11  SampleC Dark    Low     -49     0.000000
12  SampleD Dark    High    -49     8.792096
...etc...

and I want to plot the data. This works fine in every aspect other than the x axis. If I don't include the 'as.numeric()' in the x-axis call then it determines my axis is a discrete variable. My basic code is:

p <- ggplot(data=x.melt, aes(x=as.numeric(variable), y=value, colour=ExpA))
p + 
  stat_smooth(method="loess", span=0.05, se=TRUE)

Which as you can just about see, starts the x axis from zero rather than the -50. ggplot

Is there a way to correctly interpret the variable column for the axis call?

Upvotes: 0

Views: 1718

Answers (1)

blakeoft
blakeoft

Reputation: 2400

I'm assuming that the column called variable is a factor. When you convert a factor to numeric, you're telling R that you want to replace the value with the index of that value in the factor's levels. For example, let

a <- c(3, -1, 1)
b <- factor(a)
b
# [1] 3  -1 1 
# Levels: -1 1 3

Notice how it sorts the levels. So when you call as.numeric, it will replace any value of -1 with 1, because -1 is the first level, and so on. See this

as.numeric(b)
# [1] 3 1 2

There are ways convert a factor to a numeric vector and preserve the values. An efficient way is to do this

as.numeric(levels(b))[b]
# [1]  3 -1  1

And to address your specific problem, try

p <- ggplot(data=x.melt, aes(x=as.numeric(levels(variable))[variable], y=value, colour=ExpA))
p + stat_smooth(method="loess", span=0.05, se=TRUE)

Upvotes: 1

Related Questions