使用ggplot2绘图:错误:提供给连续缩放的离散值 [英] Plotting with ggplot2: Error: Discrete value supplied to continuous scale

查看:5528
本文介绍了使用ggplot2绘图:错误:提供给连续缩放的离散值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$>我的数据框的 C $ C>> dput(head(df))
structure(list(`10` = c(0,0,0,0,0,0),`33.95` = c(0,0,
0, (0,0,0,0),`58.66` = c(0,0,0,0,0,0),`84.42` = c(0,0,
0,0,0,0),` 110.21'= c(0,0,0,0,0,0),'134.16'= c(0,
0,0,0,0,0),'164.69'= c(0,0 ,0,0,0,0),`199.1'= c(0,
0,0,0,0,0),`234.35'= c(0,0,0,0,0,0 ),`257.19` = c(0,
0,0,0,0,0),`361.84` = c(0,0,0,0,0,0),`432.74` = c( 0,
0,0,0,0,0),`506.34` = c(1,0,0,0,0,0),`581.46` = c(0,
0, (0,0,0,0,0),651.71 = c(0,0,0,0,0,0),732.59 = c(0,
0,0,0,0,1) ,'817.56'= c(0,0,0,1,0,0),'896.24'= c(0,
0,0,0,0,0),'971.77'= c(0 ,1,1,1,0,1),`1038.91'= c(0,
0,0,0,0,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.na mes = c(Merc,
Peug,Fera,Fiat,Opel,Volv
),class =data.frame)

这就是我用于绘图的代码:

<$ p $ (dF,id.vars ='MW')
ggplot(meltDF [meltDF $ value == 1,])+ geom_point(aes)(p ##绘图
meltDF = (x = MW,y =变量))+
scale_x_continuous(limits = c(0,1200),breaks = c(0,400,800,1200))+
scale_y_continuous(limits = c 0,1200),break = c(0,400,800,1200))

最后一个我得到的错误:
$ b $ p $ 错误:提供给连续比例的离散值



你知道这段代码有什么问题吗?
它工作正常,直到我尝试更改比例尺,所以我相信错误在那里...



我试图找出是否有人有类似问题,因为已经有这样的话题,但它看起来像提到的其他问题。



这是如何绘制看起来像添加比例之前:



因子类型的变量不能有连续的缩放比例。您可以在定义 meltDF 之后将因子更改为 numeric code $>变量。

$ $ $ $ $ $ $ $ $ $ $ $ $变量$变量= as.numeric(水平(meltDF $变量))[meltDF $变量]

然后执行 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))

您将拥有您的图表。



希望这有助于

That's a head of my data frame which I would like to plot.

> dput(head(df))
structure(list(`10` = c(0, 0, 0, 0, 0, 0), `33.95` = c(0, 0, 
0, 0, 0, 0), `58.66` = c(0, 0, 0, 0, 0, 0), `84.42` = c(0, 0, 
0, 0, 0, 0), `110.21` = c(0, 0, 0, 0, 0, 0), `134.16` = c(0, 
0, 0, 0, 0, 0), `164.69` = c(0, 0, 0, 0, 0, 0), `199.1` = c(0, 
0, 0, 0, 0, 0), `234.35` = c(0, 0, 0, 0, 0, 0), `257.19` = c(0, 
0, 0, 0, 0, 0), `361.84` = c(0, 0, 0, 0, 0, 0), `432.74` = c(0, 
0, 0, 0, 0, 0), `506.34` = c(1, 0, 0, 0, 0, 0), `581.46` = c(0, 
0, 0, 0, 0, 0), `651.71` = c(0, 0, 0, 0, 0, 0), `732.59` = c(0, 
0, 0, 0, 0, 1), `817.56` = c(0, 0, 0, 1, 0, 0), `896.24` = c(0, 
0, 0, 0, 0, 0), `971.77` = c(0, 1, 1, 1, 0, 1), `1038.91` = c(0, 
0, 0, 0, 0, 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")

That's the code which I am using for plotting:

## 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), breaks=c(0, 400, 800, 1200))

And as the last the error which I get:

Error: Discrete value supplied to continuous scale

Do you have any idea what's wrong with this code ? It works fine until I try to change the scale so I believe that error is somewhere there...

I tried to figure out if someone has a similar problem because there is already couple of topic like that but it looks like the other problems were mentioned.

That's how to plot look like before adding scale:

解决方案

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

这篇关于使用ggplot2绘图:错误:提供给连续缩放的离散值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆