gpt4 book ai didi

r - 使用 ggplot2 绘图 : "Error: Discrete value supplied to continuous scale" on categorical y-axis

转载 作者:行者123 更新时间:2023-12-02 20:50:46 34 4
gpt4 key购买 nike

下面的绘图代码给出错误:提供给连续刻度的离散值

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

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

最佳答案

正如评论中提到的,因子类型的变量不能连续缩放。您可以在定义 meltDF 变量之后将factor 更改为numeric,如下所示。

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

然后你就会得到你的图表。

希望这有帮助

关于r - 使用 ggplot2 绘图 : "Error: Discrete value supplied to continuous scale" on categorical y-axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29278153/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com