Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
899 views
in Technique[技术] by (71.8m points)

r - ggplot2 error : Discrete value supplied to continuous scale

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...