使用ggplot2绘图:“Error: Discrete value supplied to continuous scale”在分类y轴上。

77
下面的绘图代码出现了“错误:提供了离散值以连续比例尺”的代码。
这段代码有什么问题? 在我尝试更改比例尺时它正常工作,所以错误就在那里... 我试图从类似问题中找到解决方案,但失败了。
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
  )
)

绘图代码:
## 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))

在添加比例尺之前,这是图表的样子:

Plot


12
你的y值(变量)是因子,所以你不能使用scale_y_continuous - Didzis Elferts
1
有什么快速的解决方案可以将其转换为数字或所需的解决方案吗?谢谢! - Rechlay
1
https://dev59.com/U3A75IYBdhLWcg3wOGLS - Pewi
1
尝试了几种解决方案,但都不起作用或者我做错了。 - Rechlay
3个回答

58

如评论所述,在factor类型的变量上不能有连续比例尺。您可以按照以下方式将factor更改为numeric,只需在定义meltDF变量之后执行即可。

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

然后,执行ggplot命令

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

然后您将拥有您的图表。

希望这能帮到您。


31

如果x是数字,则添加scale_x_continuous(); 如果x是字符/因子,则添加scale_x_discrete()。这可能会解决您的问题。


7

在我的情况下,您需要将该列(您认为这一列是数值型,但实际上不是)转换为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))
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接