Visualisations - code errors I cannot fix myself



I am new to R and testing new visualisations. I have found this code:


ggplot(midwest, aes(x=area, y=poptotal)) + 
  geom_point(aes(col=state, size=popdensity)) +   # draw points
  geom_smooth(method="loess", se=F) + 
  xlim(c(0, 0.1)) + 
  ylim(c(0, 500000)) +   # draw smoothing line
  geom_encircle (aes(x=area, y=poptotal), 
                expand=0.08) +   # encircle
  labs(subtitle="Area Vs Population", 
       title="Scatterplot + Encircle", 
       caption="Source: midwest")

but, after running the code I have following error:

Error in geom_encircle(aes(x = area, y = poptotal), data = midwest_select, : could not find function "geom_encircle"

then, I found another code with animations:

# Source:
# install.packages("cowplot")  # a gganimate dependency
# devtools::install_github("dgrtwo/gganimate")
theme_set(theme_bw())  # pre-set the bw theme.

g <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, frame = year)) +
  geom_point() +
  geom_smooth(aes(group = year), 
              method = "lm", 
              show.legend = FALSE) +
  facet_wrap(~continent, scales = "free") +
  scale_x_log10()  # convert to log scale

gganimate(g, interval=0.2)

unfortunately, error again:
Error in gganimate(g, interval = 0.2) : could not find function "gganimate"

How can I fix the errors above? Can anyone help?


gganimate() isn't actually a function. It's the name of the package, but not a function in that package (in the same way dplyr() isn't a function in the dplyr package).

You can see the full list of functions in the package documentation in R by typing ?gganimate-package after loading the library (with library(gganimate)).


Actually (and this is really confusing), gganimate() was the main workhorse function in an older version of the package gganimate. For instance, see this documentation:

But now the API has changed substantially:

So there are still examples out there on the internet that use the old way of doing things. To make those examples work, you have to install a specifically tagged GitHub release of gganimate (more info at the second link, above).

It’s probably better, though, to look for examples using the new API, since the old one is dead and gone now.


The example won’t run for me because I don’t have the code creating midwest_select, but geom_encircle() is definitely a function in ggalt. Maybe try reinstalling ggalt?


Thank you very much for all your responses :slight_smile: