Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 error : Discrete value supplied to continuous scale

Tags:

r

ggplot2

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?

like image 323
nan Avatar asked Sep 19 '14 15:09

nan


People also ask

How do you fix discrete value supplied to continuous scale?

This error can happen if you use character or factor type for the y variable in your data. You can solve this error by using numeric values instead of character or factor. Alternatively, you can subset the data frame before plotting the data.

What does error discrete value supplied to continuous scale mean?

The “discrete value supplied to continuous scale” error message is more of a minor nuisance than a serious problem. It is simply a matter of using the wrong vector from the data frame. Correcting that one continuous data mistake in your plotting code solves the problem in a simple and easy manner.


2 Answers

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.

like image 162
eipi10 Avatar answered Oct 07 '22 12:10

eipi10


Now that we know the two color vars are of different types, there's the issue. You can try using a different scale for one (e.g. fill instead of color)

set.seed(123)
my_df1 <- data.frame(a=rnorm(100), b=runif(100), c=rep(1:10, 10))
my_df2 <- data.frame(a=rnorm(100), b=runif(100), c=factor(rep(LETTERS[1:5], 20)))
 
# this won't work. can't assign discrete and continuous to same scale
ggplot() +
  geom_point(data=my_df1, aes(x=a, y=b, color=c)) +
  geom_polygon(data=my_df2, aes(x=a, y=b, color=c), alpha=0.5)

Error: Discrete value supplied to continuous scale

# but use fill for polygons, and that works:
ggplot() +
  geom_point(data=my_df1, aes(x=a, y=b, color=c)) +
  geom_polygon(data=my_df2, aes(x=a, y=b, fill=c), alpha=0.5)

plot output

If you have to use the same scale (color), and can't convert the variables to the same type, see this for more info: Plotting continuous and discrete series in ggplot with facet

like image 24
arvi1000 Avatar answered Oct 07 '22 11:10

arvi1000