Ggplotly fails to run, "Error: No Cairo or bitmap device is available"

Hello,

I’m currently on macOS Catalina and trying to run a very simple .R file (that will eventually lead to a DashR app. My script currently contains some ggplot code and then turning it into a Plotly object using the ggplotly function.

My ggplot code works fine and shows the correct plot:

library(tidyverse)
district_density <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/district_density.csv",
                             stringsAsFactors = FALSE)
district_density$cluster <- factor(district_density$cluster, levels=c("Pure urban", "Urban-suburban mix", "Dense suburban", "Sparse suburban", "Rural-suburban mix", "Pure rural"))
district_density$region <- factor(district_density$region, levels=c("West", "South", "Midwest", "Northeast"))

p <- ggplot(district_density,aes(x=cluster, y=dem_margin, colour=region)) +
  geom_jitter(aes(text=paste("district: ", cd_code)), width=0.25, alpha=0.5, ) +
  geom_hline(yintercept=0) +
  theme(axis.text.x = element_text(angle = -30, hjust = 0.1)) +
  labs(title = "Democratic performance in the 2018 House elections, by region and density",
       x = "Density Index from CityLab",
       y = "Democratic Margin of Victory/Defeat")
p

But when I apply ggplotly() to p, I get an error:

ggplotly(p)

Error: No Cairo or bitmap device is available. Such a graphics device is required to convert sizes correctly in ggplotly().

 You have two options:
  (1) install.packages('Cairo')
  (2) compile R to use a bitmap device (png or jpeg)

Of course I dutifully then tried to install Cairo, first using brew (1.16.0_2; successfully installed, got same error when running the script) and then again through an R console from cran (1.15.10) and got this error:

... [install logs truncated till close to failure point]
checking whether fontconfig/freetype2 location can be guessed... possibly
checking whether additional flags work... no
configure: error: Cannot use cairo-ft backend, although cairo claims it is working. Please check your caito installation and/or update cairo if necessary or set CAIRO_CFLAGS/CAIRO_LIBS accordingly.
ERROR: configuration failed for package ‘Cairo’
* removing ‘/usr/local/lib/R/3.6/site-library/Cairo’

Any ideas what the problem could be?

I tried googling and there was a recommendation to install Xquartz, which I then did and the script still ailed to run (with the same error message).