Smoothen rasterized data points

I have created a raster using lon lat coordinates and values from a cluster analysis. The code below produces the output I want but it has some salt and pepper effects which I do not want (see attached picture). I am not sure what is causing this. Is there a way to remove these effects and make the plot look smoother? I tried to use the focal function under the raster package to solve this but it did not work out well.

############
library(raster)
library(dplyr)
library(reprex)

xy <- readRDS("crop.xy.rds")
class <- readRDS("hc_em10.rds")

xy <- as.data.frame(xy)
xy = bind_cols(xy, class = class$classification)
head(xy)

#Convert object of interest to spatial points data frame

coordinates(xy) <- ~ lon+lat
projection(xy) <- "+proj=longlat +ellps=WGS84 +no_defs"

#create empty raster

r <-raster( nrows = 1217, ncols = 1047,
            xmn=11.74, xmx=33.05, ymn=-34.82, ymx=-15.61, 
            crs="+proj=longlat +ellps=WGS84 +no_defs ", 
            vals=NA) 

##Rasterize and plot

ras <- rasterize(xy, r, field=as.numeric(xy$class))
levels(ras) <- data.frame(ID = unique(ras), class = unique(ras))
plot(ras)

#r_focal = focal(ras, w = matrix(1, nrow = 3, ncol = 3), fun = modal, na.rm=TRUE)
#plot(r_focal)

###############

Created on 2021-07-21 by the reprex package (v1.0.0)

![Rplot|690x322](upload://41FeaarOiSXDVufWlU0lE8fJQvt.png)

This post was flagged by the community and is temporarily hidden.