83

The plotting code below gives Error: Discrete value supplied to continuous scale

What's wrong with this code? It works fine until I try to change the scale so the error is there... I tried to figure out solutions from similar problem but couldn't.

meltDF <- data.frame(
  MW = c(
    3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9,
    9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4,
    8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4,
    7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9,
    6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4,
    3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9,
    9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4, 8.1, 9, 9.4, 3.9, 6.4, 7.4,
    8.1, 9, 9.4
  ),
  variable = factor(
    c(
      "10", "10", "10", "10", "10", "10", "33.95", "33.95", "33.95",
      "33.95", "33.95", "33.95", "58.66", "58.66", "58.66", "58.66",
      "58.66", "58.66", "84.42", "84.42", "84.42", "84.42", "84.42",
      "84.42", "110.21", "110.21", "110.21", "110.21", "110.21", "110.21",
      "134.16", "134.16", "134.16", "134.16", "134.16", "134.16", "164.69",
      "164.69", "164.69", "164.69", "164.69", "164.69", "199.1", "199.1",
      "199.1", "199.1", "199.1", "199.1", "234.35", "234.35", "234.35",
      "234.35", "234.35", "234.35", "257.19", "257.19", "257.19", "257.19",
      "257.19", "257.19", "361.84", "361.84", "361.84", "361.84", "361.84",
      "361.84", "432.74", "432.74", "432.74", "432.74", "432.74", "432.74",
      "506.34", "506.34", "506.34", "506.34", "506.34", "506.34", "581.46",
      "581.46", "581.46", "581.46", "581.46", "581.46", "651.71", "651.71",
      "651.71", "651.71", "651.71", "651.71", "732.59", "732.59", "732.59",
      "732.59", "732.59", "732.59", "817.56", "817.56", "817.56", "817.56",
      "817.56", "817.56", "896.24", "896.24", "896.24", "896.24", "896.24",
      "896.24", "971.77", "971.77", "971.77", "971.77", "971.77", "971.77",
      "1038.91", "1038.91", "1038.91", "1038.91", "1038.91", "1038.91"
    ),
    levels = c(
      "10", "33.95", "58.66", "84.42", "110.21", "134.16", "164.69",
      "199.1", "234.35", "257.19", "361.84", "432.74", "506.34", "581.46",
      "651.71", "732.59", "817.56", "896.24", "971.77", "1038.91"
    )
  ),
  value = c(
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0
  )
)

The plotting code:

## Plotting
ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y = variable)) +
  scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
  scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))

Here's how the plot looked before adding scale:

Plot

4
  • 12
    Your y values (variable) are factor, so you can't use scale_y_continuous. Commented Mar 26, 2015 at 12:21
  • 1
    Any quick solution how to change it into numeric or the one which is required ? Thanks! Commented Mar 26, 2015 at 12:23
  • 1
    stackoverflow.com/questions/3418128/… Commented Mar 26, 2015 at 12:45
  • 1
    Tried couple of solution but it doesn't work or I do it incorrectly. Commented Mar 26, 2015 at 13:29

3 Answers 3

60

As mentioned in the comments, there cannot be a continuous scale on variable of the factor type. You could change the factor to numeric as follows, just after you define the meltDF variable.

meltDF$variable=as.numeric(levels(meltDF$variable))[meltDF$variable]

Then, execute the ggplot command

  ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y =   variable)) +
     scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
     scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))

And you will have your chart.

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

35

if x is numeric, then add scale_x_continuous(); if x is character/factor, then add scale_x_discrete(). This might solve your problem.

Comments

8

In my case, you need to convert the column(you think this column is numeric, but actually not) to numeric

geom_segment(data=tmpp, 
   aes(x=start_pos, 
   y=lib.complexity, 
   xend=end_pos, 
   yend=lib.complexity)
)
# to 
geom_segment(data=tmpp, 
   aes(x=as.numeric(start_pos), 
   y=as.numeric(lib.complexity), 
   xend=as.numeric(end_pos), 
   yend=as.numeric(lib.complexity))
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.