Error could not find function "ggplot" following instructions in "Bivarite Mapping with ggplot2. "

I've followed instructions in the above mentioned article.

install.packages("sf")
install.packages("biscale")
library(biscale)
data <- bi_class(stl_race_income, x = pctWhite, y = medInc, style = "quantile", dim = 3)
map <- ggplot() +
geom_sf(data = data, mapping = aes(fill = bi_class), color = "white", size = 0.1, show.legend = FALSE) +
bi_scale_fill(pal = "DkBlue", dim = 3) +
labs(
title = "Race and Income in St. Louis, MO",
subtitle = "Dark Blue (DkBlue) Palette"
) +
bi_theme()

Above results in: Error in ggplot() : could not find function "ggplot"
How do I fix this? One googled suggestion was to install colorspace, which I did, to no avail.

You also need to load ggplot2:

library("ggplot2")

If you get a "package not found" error, you can also install it as above:

install.packages("ggplot2")

Thanks. I am 100% new to R.

I was able to continue until the last line:

legend <- bi_legend(pal = "DkBlue",

  •                 dim = 3,
    
  •                 xlab = "Higher % White ",
    
  •                 ylab = "Higher Income ",
    
  •                 size = 8)
    

finalPlot <- ggdraw() +

  • draw_plot(map, 0, 0, 1, 1) +
  • draw_plot(legend, 0.2, .65, 0.2, 0.2)
    Error in ggdraw() : could not find function "ggdraw"

I notice the leading + have been changed to dots!

Google suggests that function comes from the package cowplot (which is common to combine separate ggplots).

You didn't link the "above mentioned article" in your first post. Did that article not mention all the packages that would be needed?

I notice the leading + have been changed to dots!

This is a convenience from this forum, if you start lines with + or * it automatically guesses you're trying to make a list.

Link to article: https://slu-opengis.github.io/biscale/articles/biscale.html#getting-help-1

No it did not mention all the packages.

I will use Preformatted text from now on.

I finally got a map to draw. Thanks for your help.

Indeed, it seems @chris.prener overlooked the initial library loadings in the tutorial.

Happy that it worked in the end!

1 Like

I did! Sorry that gave you brief, @bgailer, and hope the rest of your mapping is more straight forward. I've opened an issue on the package to add these missing packages to the tutorial!

Thanks for tagging me into this @AlexisW!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.