0

when I tried to plot a graph of decision boundary in R, I met some problem and it returned a error "Continuous value supplied to discrete scale". I think the problem happened in the scale_colur_manual but I don't know how to fix it. Below is the code attached.

library(caTools)
set.seed(123)
split = sample.split(df$Purchased,SplitRatio = 0.75)
training_set = subset(df,split==TRUE)
test_set = subset(df,split==FALSE)

# Feature Scaling
training_set[,1:2] = scale(training_set[,1:2])
test_set[,1:2] = scale(test_set[,1:2])

# Fitting logistic regression to the training set
lr = glm(formula = Purchased ~ .,
         family = binomial,
         data = training_set)

 #Predicting the test set results
 prob_pred = predict(lr,type = 'response',newdata = test_set[-3])
 y_pred = ifelse(prob_pred > 0.5, 1, 0)

 #Making the Confusion Matrix
 cm = table(test_set[,3],y_pred)
 cm

 #Visualizing the training set results
 library(ggplot2)

 set = training_set

 X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)

 X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)

 grid_set = expand.grid(X1, X2)

 colnames(grid_set) = c('Age', 'EstimatedSalary')

 prob_set = predict(lr, type = 'response', newdata = grid_set)

 y_grid = ifelse(prob_set > 0.5, 1,0)


 ggplot(grid_set) +

       geom_tile(aes(x = Age, y = EstimatedSalary, fill = factor(y_grid)),

       show.legend = F) +

 geom_point(data = set, aes(x = Age, y = EstimatedSalary, color = Purchased),

         show.legend = F) +

 scale_fill_manual(values = c("orange", "springgreen3")) +

 scale_colour_manual(values = c("red3", "green4")) +

 scale_x_continuous(breaks = seq(floor(min(X1)), ceiling(max(X2)), by = 1)) +

 labs(title = "Logistic Regression (Training set)",

      ylab = "Estimated Salary", xlab = "Age")
  • 6
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Remove any code not directly necessary to reproduce the problem. – MrFlick Feb 16 '20 at 03:00
  • 1
    Your error is not from `scale_color_manual` but rather in the `scale_x_continuous`. If I have to guess, I will say that either your variable "Age" is not in a numerical format or your X1 and X2 are not in numerical format either (but I'm guessing on the first option). As asked by MrFlick, you should provide a reproducible example of your dataset. – dc37 Feb 16 '20 at 03:31

1 Answers1

0

Is your Purchased variable a factor? If not, it has to be. Try this:

grid_set %>%
   mutate(Purchased=factor(Purchased)) %>%
   ggplot() + 
   geom_tile(aes(x = Age, y = EstimatedSalary, fill = factor(y_grid)),
       show.legend = F) + ... # add the rest of your commands.
Edward
  • 10,360
  • 2
  • 11
  • 26