So I'm creating and SVM model and then trying to create a prettier version of the plot with a log scale in the x axis. When I map my predictions to a grid, for some reason it seems to be mirror imaged and I can't figure out why. Does anyone have any ideas what's going wrong?
The SVM model is as follows:
#SVM
dat <- select(data, Soft.or..Hybrid., Width..m.,
Hi..cm., Kt, Submergence) #trim to desired columns
dat <- dat[complete.cases(dat),]
dat$Soft.or..Hybrid. <- as.factor(dat$Soft.or..Hybrid.) #convert soft/hybrid to a factor (SVM requires numeric response var)
dat$Submergence <- as.factor(dat$Submergence)
n <- nrow(dat) #number of observations
ntrain <- round(n*0.80) #80% for training set
set.seed(314)
tindex <- sample(n, ntrain) #create random index
train_data <- dat[tindex,] #create training set
test_data <- dat[-tindex,] #create test set
svm1 <- svm(Soft.or..Hybrid.~., data=train_data,
method="C-classification", kernel="radial",
gamma=0.1, cost=100)
summary(svm1)
plot(svm1, train_data, Kt~Width..m.,log ="x", ylim = c(0, 1), xlim = c(0,1000)) #basic plot
This gives me the following plot with the results I expect:

Then to create a prettier plot I'm doing this:
#nicer plot
col2 <- seq(log(min(dat$Width..m.)), log(1000), length.out = 50)
col3 <- seq(min(dat$Hi..cm.), max(dat$Hi..cm.), length.out = 2)
col4 <- seq(min(dat$Kt), max(dat$Kt), length.out = 50)
grid_log <- expand.grid(#Soft.or..Hybrid. = col1,
Width..m. = exp(col2),
Hi..cm. = col3,
Kt = col4,
Submergence = dat$Submergence)
ygrid = predict(svm1, grid_log) #map predictions to grid
# add bayes decision boundary
func <- predict(svm1, grid_log, decision.values = TRUE)
func <- attributes(func)$decision
x_variable <- "Width..m."
y_variable <- "Kt"
# plot
ggplot(grid_log, aes(x = grid_log[, x_variable], y = grid_log[, y_variable], color = as.factor(ygrid))) +
geom_point(size = 1, alpha = 0.2) +
geom_point(data = dat, aes(x = Width..m., y = Kt, color = Soft.or..Hybrid.), shape = 8) +
# contour plot for level = 0
geom_contour(aes(z = as.numeric(func)),
breaks = 0, color = "black", linetype = "solid") +
# Contour plot for level = 0.5
geom_contour(aes(z = as.numeric(func)),
breaks = 0.5, color = "black", linetype = "solid") +
scale_color_manual(values = c("red", "blue", "green")) + # Adjust color mapping as needed
ylim(0,1) +
labs(title = "SVM Predictions",
x = x_variable,
y = y_variable) +
scale_x_log10()
But when I map the predictions to a grid, it seems to mirror image itself and I'm not sure why... if anyone has suggestions on what to do to fix this I'd greatly appreciate it!
