'Why is the SVG of my raster plot so blurry?

I'm trying to print a raster visualisation. It renders fine in RStudio, but when I save it using the base svg device it comes out super blurry - as though each square of the raster is a massive pixel and they've been interpolated at some stage in the process.

Here's a reprex:

## libraries / set up
require(dplyr)
require(ggplot2)

set.seed(11)

## Generate data
### Make 100 random(ish) pairings of eye and hair colour to visualise
possibleEye <- c("green", "green", "green", 
                          "blue", "blue", "brown", "hazel")
possibleHair <- c("blonde", "blonde", "brown",
                  "gray", "gray", "gray", "ginger")
eyeColours <- sample(possibleEye, 100, replace = TRUE)
hairColours <- sample(possibleHair, 100, replace = TRUE)

plotData <- tibble(
  eye.colour = factor(eyeColours),
  hair.colour = factor(hairColours)
)

### Count the occurrences of each combination
plotData <- 
  plotData |> 
  group_by(hair.colour, eye.colour) |> 
  count()

## Plot raster

rasterPlot <- 
  ggplot(plotData, aes(x = eye.colour, y = hair.colour, fill = n)) +
  geom_raster()

rasterPlot

## save SVG file

svg("my-raster.svg")
print(rasterPlot)
dev.off()
#> png 
#>   2

Created on 2022-02-07 by the reprex package (v2.0.1)

But this is what my SVG looks like:

svg print of raster image, in which the squares are blurry

Why is that happening? How can I stop it from happening?

It's not a fault with the software I'm using to view the SVG, because it looks the same when viewed in either Inkscape or Google Chrome.

Here's my R.Version():

$platform
[1] "x86_64-w64-mingw32"

...

$version.string
[1] "R version 4.1.1 (2021-08-10)"

$nickname
[1] "Kick Things"


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source