77

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

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Rechlay
  • 1,457
  • 2
  • 12
  • 19

3 Answers3

57

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

smci
  • 32,567
  • 20
  • 113
  • 146
Nikos
  • 3,267
  • 1
  • 25
  • 32
30

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

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
yc_hello
  • 462
  • 4
  • 5
7

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))
)
Mitoxys
  • 297
  • 3
  • 8