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

前端之家收集整理的这篇文章主要介绍了用ggplot2绘图:在分类y轴上绘制“错误:提供给连续刻度的离散值”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的绘图代码给出了误差:提供给连续刻度的离散值

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

这是我的数据的负责人:

> dput(head(df))
structure(list(`10` = c(0,0),`33.95` = c(0,`58.66` = c(0,`84.42` = c(0,`110.21` = c(0,`134.16` = c(0,`164.69` = c(0,`199.1` = c(0,`234.35` = c(0,`257.19` = c(0,`361.84` = c(0,`432.74` = c(0,`506.34` = c(1,`581.46` = c(0,`651.71` = c(0,`732.59` = c(0,1),`817.56` = c(0,1,`896.24` = c(0,`971.77` = c(0,`1038.91` = c(0,MW = c(3.9,6.4,7.4,8.1,9,9.4)),.Names = 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","MW"),row.names = c("Merc","Peug","Fera","Fiat","Opel","Volv"
),class = "data.frame")

绘图代码

## Plotting
meltDF = melt(df,id.vars = 'MW')
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))

这是在添加比例之前情节的样子:

解决方法

正如评论中所提到的,因子类型的变量不可能有连续的比例。在定义meltDF变量之后,您可以将因子更改为数字,如下所示。
meltDF$variable=as.numeric(levels(meltDF$variable))[meltDF$variable]

然后,执行ggplot命令

ggplot(meltDF[meltDF$value == 1,y =   variable)) +
     scale_x_continuous(limits=c(0,1200)) +
     scale_y_continuous(limits=c(0,1200))

你会得到你的图表。

希望这可以帮助

猜你在找的CSS相关文章