使用 ggplot2 绘图:分类 y 轴上的“错误:提供给连续刻度的离散值”

问题描述 投票:0回答:3

下面的绘图代码给出了

Error: Discrete value supplied to continuous scale

这段代码有什么问题?它工作正常,直到我尝试更改比例所以错误就在那里......我试图从类似的问题中找出解决方案但不能。

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

r ggplot2 scale categorical-data r-factor
3个回答
56
投票

如评论中所述,不能对

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

你会得到你的图表。

希望这有帮助


28
投票

如果

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


6
投票

在我的例子中,您需要将列(您认为该列是数字,但实际上不是)转换为

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))
)
© www.soinside.com 2019 - 2024. All rights reserved.