ggplot2错误:提供给连续标度的离散值 [英] ggplot2 error : Discrete value supplied to continuous scale

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

问题描述

我有一个名为merged的数据集,它包含3个数字列pauseMedian和numTotalPauses和diff。我也有一个splineHull数据集,它还包含数字列pauseMedian和numTotalPauses,再加上一个6级因子microstyle

我有以下代码,这完美地工作。它绘制一个散点图,然后用splineHull多边形覆盖,这个多边形是根据microstyle着色的。

脚本1:


$ b $

  ggplot(data = merged,aes(x = pauseMedian,y = numTotalPauses))
+ geom_point()
+ geom_polygon data = splineHull,
mapping = aes(x = pauseMedian,
y = numTotalPauses,
group = microstyle,
color = microstyle),
alpha = 0)

然后,我还想通过添加一个属性color =来改变散点图中点的颜色

脚本2:

  ggplot(data = merged,aes(x = pauseMedian,y = numTotalPauses,color = diff))
+ geom_point()
+ geom_polygon(data = splineHull,
mapping = aes(x = PAUS eMedian,
y = numTotalPauses,
group = microstyle,
color = microstyle),
alpha = 0)

我看到以下错误:

 错误:提供给连续的离散值规模

我不知道为什么我会看到这个错误。如果我仍然想要彩色散点图但没有多边形,我运行下面的代码再次运行。



脚本3:

  ggplot(data = merged,aes(x = pauseMedian,y = numTotalPauses,color = diff))
+ geom_point()

那么,脚本2 发生了什么,错误来自哪里,以及我可以使它工作吗?

解决方案

很明显,对于两种不同的geoms,你不能有不同的颜色美学。作为解决方法,请使用点的填充美学。这意味着您必须使用具有内部填充的点标记样式(请参阅?pch 并向下滚动以获取可用的点样式)。这是一种方法:

  ggplot()+ 
geom_point(data = merged,aes(x = pauseMedian ,y = numTotalPauses,fill = diff),
pch = 21,size = 5,color = NA)+
geom_polygon(data = splineHull,
mapping = aes(x = pauseMedian,
y = numTotalPauses,
color = microstyle),
alpha = 0)



<添加 color = NA (在 aes()之外),去掉点周围的默认黑色边框标记。如果你想在点附近有一个彩色边框,只需将 color = NA 更改为你喜欢的任何颜色即可。

请参阅 ggplot2 Google的此主题讨论类似的问题和一些解决方法。


I have a dataset called "merged", which contains 3 numeric columns "pauseMedian" and "numTotalPauses" and "diff". I also have a splineHull dataset, which also contains numeric columns "pauseMedian" and "numTotalPauses", plus a 6-level factor "microstyle"

I have the following code, which works perfectly. It plots a scatter plop and then overlay it with splineHull polygons colored according to "microstyle".

script 1:

ggplot(data=merged,aes(x = pauseMedian, y = numTotalPauses)) 
       + geom_point()  
       + geom_polygon(data = splineHull, 
                      mapping=aes(x=pauseMedian, 
                                  y=numTotalPauses, 
                                  group=microstyle, 
                                  color = microstyle),
                       alpha=0)

Then, I also want to change the color of the points in the scatter plot by adding just one attribute color = diff.

script 2:

ggplot(data=merged,aes(x = pauseMedian, y = numTotalPauses, color = diff)) 
       + geom_point()  
       + geom_polygon(data = splineHull, 
                      mapping=aes(x=pauseMedian, 
                                  y=numTotalPauses, 
                                  group=microstyle, 
                                  color = microstyle),
                       alpha=0)

I see the following error:

Error: Discrete value supplied to continuous scale

I don't know why I see this error. If I still want colored scatter plot but no polygons, I run the following code it works again.

script 3:

ggplot(data=merged,aes(x = pauseMedian, y = numTotalPauses, color = diff)) 
       + geom_point()  

So, what happened with script 2, where is the error from, and how can I make it work?

解决方案

Evidently, you can't have different color aesthetics for two different geoms. As a workaround, use a fill aesthetic for the points instead. This means you have to use a point marker style that has a filled interior (see ?pch and scroll down for the available point styles). Here's a way to do that:

ggplot() + 
  geom_point(data=merged,aes(x = pauseMedian, y = numTotalPauses, fill = diff),
             pch=21, size=5, colour=NA) +
  geom_polygon(data = splineHull, 
               mapping=aes(x=pauseMedian, 
                           y=numTotalPauses, 
                           colour = microstyle),
               alpha=0) 

Adding colour=NA (outside of aes()), gets rid of the default black border around the point markers. If you want a colored border around the points, just change colour=NA to whatever colour you prefer.

Also see this thread from the ggplot2 Google group, discussing a similar problem and some workarounds.

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

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