Plotting with ggplot2: “Error: Discrete value supplied to continuous scale” on categorical y-axis

后端 未结 3 539
一生所求
一生所求 2020-11-29 04:26

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 s

相关标签:
3条回答
  • 2020-11-29 04:45

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

    0 讨论(0)
  • 2020-11-29 04:54

    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

    0 讨论(0)
  • 2020-11-29 04:55

    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))
    )
    
    0 讨论(0)
提交回复
热议问题