Error: Discrete value supplied to continuous scale on RSTUDIO

I keep getting the Error: Discrete value supplied to continuous scale for the following code

ggplot(ENGR_293_TEST, aes(x="X", y="Y")) +
annotation_custom(court, 0, 50, 0, 47) +
geom_point(aes(colour = "red", shape = Result)) + scale_shape_identity() +
xlim(0, 50) +
ylim(0, 47)

Here is the data

Column names inside of ggplot() so not need to be quoted. I also removed the call to annotation_custom() since the court variable is not available.

DF <- data.frame(X = c(7,47,10,26,6,30,25,23,22,29),
                 Y = c(13,6,18,20,4,25,9,6,6,25),
                 Result = c(1.0,1,0,0,0,1,0,1,0, 1),
                 Pxy = c(1.0, 0.5, 0, 0.5, 0, 0.5, 0.5, 1.0, 0.5, 1.0))
library(ggplot2)
ggplot(DF, aes(x=X, y=Y)) +
  #annotation_custom(court, 0, 50, 0, 47) +
  geom_point(aes(colour = "red", shape = Result)) + scale_shape_identity() +
  xlim(0, 50) +
  ylim(0, 47)

Created on 2019-11-24 by the reprex package (v0.2.1)

Thank you so much but I now get a new error

> DF <- data.frame(X = c(7,47,10,26,6,30,25,23,22,29),
+                  Y = c(13,6,18,20,4,25,9,6,6,25),
+                  Result = c(1.0,1,0,0,0,1,0,1,0, 1),
+                  Pxy = c(1.0, 0.5, 0, 0.5, 0, 0.5, 0.5, 1.0, 0.5, 1.0))
> ggplot(DF, aes(x=X, y=Y)) +
+     annotation_custom(court, 0, 50, 0, 47) +
+     geom_point(aes(colour = "red", shape = Result)) + scale_shape_identity() +
+     xlim(0, 50) +
+     ylim(0, 47)
Error in y[setdiff(names(y), names(x))] : 
  object of type 'closure' is not subsettable

Hi, and welcome!

Your question comes close to including a reproducible example, called a reprex, but falls a bit short. Having a reprex attracts more and helpful answers. The data don't have to be a full set, or even your actual data. They can be a built-in data set with the same structure or just a piece.

So, let's look at the screenshot. Among X, Y, Result and Pxt, which are discrete and which are continuous?

ggplot(ENGR_293_TEST, aes(x="X", y="Y"))

invokes two that take on several different values. (A rule of thumb is you can try treating as continuous any variable with more than 10 numeric values).

We can neglect annotation_custom, xlim and ylim and scale_shape_identity() for now and focus on geom_point. We see immediately that Pxy isn't implicated, so the issue comes down to Result, which indeed is discrete--1 or 0, nothing more, nothing less and nothing in between.

Why do we care?

First off, everyone working in ggplot below the Jedi level sees this a lot. Plus, of course, it's in the way of a display of your data.

Take a look at help(geom_point). Both aes arguments are optional. We can discard colour and focus on shape.

Looking at the [documentation(Differentiation related aesthetics: linetype, size, shape — aes_linetype_size_shape • ggplot2)

Shape takes four types of values: an integer in [0, 25],
a single character-- which uses that character as the plotting symbol,
a . to draw the smallest rectangle that is visible (i.e., about one pixel)
an NA to draw nothing

While Result is an integer, which one? Here, the argument being looked for is a continuous scale from 0 to 25, not a discrete scale of 0 or 1.

Let's step back though and ask what do you want the shape of the points to reflect? Is it just to distinguish zero and one in Results?

If so, does the group aes get you there?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.