Visualisations - code errors I cannot fix myself

visualization

#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: