Visualisations - code errors I cannot fix myself

visualization
gganimate
ggalt

#1

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

library(ggplot2)
library(ggalt)

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), 
                data=midwest_select, 
                color="red", 
                size=2, 
                expand=0.08) +   # encircle
  labs(subtitle="Area Vs Population", 
       y="Population", 
       x="Area", 
       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: https://github.com/dgrtwo/gganimate
# install.packages("cowplot")  # a gganimate dependency
# devtools::install_github("dgrtwo/gganimate")
library(ggplot2)
library(gganimate)
library(gapminder)
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?


#2

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)).


#3

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:
https://www.rdocumentation.org/packages/gganimate/versions/0.1.1/topics/gganimate

But now the API has changed substantially: https://github.com/thomasp85/gganimate#old-api

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.


#4

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?


#5

Thank you very much for all your responses :slight_smile:


#6

+100 to this. I've had a lot of confusion after @thomasp85's complete re-write of the package.
As the package wasn't renamed, I bet we'll keep seeing errors like this for some time, as the old gganimate code examples are forever in the ether and with no indication that it is outdated code.

Perhaps, some custom error message for any old function would help?


#7

Best practice is to use .Deprecated() to alert users a change is coming and .Defunct() when the change is made. For example:

Version 1.0

foo <- function(...) {
    ...
}

Version 1.5

foo <- function(...) {
    .Deprecated("bar")
    ...
}

foo()
# Warning message:
# 'foo' is deprecated.
# Use 'bar' instead.
# See help("Deprecated")

Version 2.0

foo <- function(...) {
    .Defunct("bar")
    ...
}

foo()
# Error: 'foo' is defunct.
# Use 'bar' instead.
# See help("Defunct")

If you feel it'd help, you could create an issue or make a pull request to add this to gganimate.