Why do i get an error that my aesthetics must be with length1 or the same as the data?

I get this error when I try to plot an animated scatter plot:
Error: Aesthetics must be either length 1 or the same as the data (1704): x, y, size, colour, frame

This is the code and my data is attached:

library(readxl)
Africa <- read_excel("C:/Users/jxt161530/Dropbox/Data Visualization/Final project/Final Presentation/Africa.xlsx")
View(Africa)

library(gapminder)
library(ggplot2)
theme_set(theme_bw())

p <- ggplot(gapminder, aes(Africa$Manuf, Africa$Rents, size = Africa$Pop, color = Africa$Country, frame = Africa$Year)) +
  geom_point() +
  scale_x_log10()
library(gganimate)
gganimate(p)
#Error message: Error: Aesthetics must be either length 1 or the same as the data (1704): x, y, size, colour, frame

The expected output is something like this:

https://github.com/dgrtwo/gganimate/blob/master/README/README-fig-unnamed-chunk-3-.gif

Also, I want to plot two lines on a graph of “Manuf” for only two countries (Ghana and Ivory Coast) and animate the two lines to look like the one in this link: http://lenkiefer.com/2018/01/17/simple-animated-line-plot/

Data:

Country Year Manuf Pop Rents Gov
Ghana 1996 4.00 9.00 4.00 7.00
Ghana 1997 4.30 10.00 6.00 6.00
Ghana 1998 3.90 11.00 7.00 7.00
Ghana 1999 3.50 12.00 5.00 5.00
Ghana 2000 3.80 13.00 4.00 6.00
Ghana 2001 4.40 14.00 3.00 5.00
Ghana 2002 5.60 15.00 2.00 6.00
Ghana 2003 5.30 16.00 8.00 8.00
Ghana 2004 5.40 17.00 9.00 9.00
Ghana 2005 6.30 18.00 8.00 7.00
Ghana 2006 7.00 19.00 6.00 8.00
Ghana 2007 -1.22 20.00 7.00 8.00
Ghana 2008 3.70 21.00 10.00 8.00
Ghana 2009 -1.31 22.00 12.00 5.00
Ghana 2010 7.60 23.00 16.00 6.00
Ghana 2011 17.00 24.00 15.00 5.00
Ghana 2012 1.95 25.00 16.00 6.00
Ghana 2013 -0.50 26.00 17.00 7.00
Ghana 2014 -0.82 27.00 16.00 8.00
Ghana 2015 2.22 28.00 14.00 9.00
Ghana 2016 2.00 29.00 16.00 8.00
Ivory_Coast 1996 14.00 6.00 1.00 4.00
Ivory_Coast 1997 15.00 7.00 1.00 5.00
Ivory_Coast 1998 12.80 8.00 2.00 6.00
Ivory_Coast 1999 14.30 9.00 2.00 5.00
Ivory_Coast 2000 14.90 10.00 1.00 3.00
Ivory_Coast 2001 10.20 11.00 3.00 4.00
Ivory_Coast 2002 9.30 12.00 2.00 2.00
Ivory_Coast 2003 6.30 13.00 5.00 6.00
Ivory_Coast 2004 4.30 14.00 3.00 7.00
Ivory_Coast 2005 3.90 15.00 4.00 6.00
Ivory_Coast 2006 2.20 16.00 2.00 5.00
Ivory_Coast 2007 -1.80 17.00 3.00 7.00
Ivory_Coast 2008 3.00 18.00 5.00 5.00
Ivory_Coast 2009 -0.33 19.00 4.00 6.00
Ivory_Coast 2010 -0.96 20.00 2.00 5.00
Ivory_Coast 2011 -11.11 21.00 6.00 5.00
Ivory_Coast 2012 6.75 22.00 5.00 6.00
Ivory_Coast 2013 18.51 23.00 7.00 5.00
Ivory_Coast 2014 4.17 24.00 2.00 7.00
Ivory_Coast 2015 2.82 25.00 4.00 5.00
Ivory_Coast 2016 6.35 26.00 6.00 8.00
Botswana 1996 -0.66 0.80 2.60 6.00
Botswana 1997 6.57 0.80 4.60 5.00
Botswana 1998 -1.53 0.89 5.60 6.00
Botswana 1999 6.67 0.89 3.60 4.00
Botswana 2000 6.81 0.90 2.60 5.00
Botswana 2001 0.41 0.90 1.60 4.00
Botswana 2002 12.75 0.95 0.60 5.00
Botswana 2003 1.84 0.95 6.60 7.00
Botswana 2004 -1.15 1.00 7.60 8.00
Botswana 2005 5.64 1.00 6.60 6.00
Botswana 2006 7.80 1.20 4.60 7.00
Botswana 2007 2.01 1.30 5.60 7.00
Botswana 2008 0.09 1.30 8.60 7.00
Botswana 2009 -27.03 1.50 10.60 4.00
Botswana 2010 13.13 1.50 14.60 5.00
Botswana 2011 1.46 1.80 13.60 4.00
Botswana 2012 0.30 1.80 14.60 5.00
Botswana 2013 16.13 2.00 15.60 6.00
Botswana 2014 -0.47 2.20 14.60 7.00
Botswana 2015 -8.94 2.20 12.60 8.00
Botswana 2016 1.28 2.30 14.60 7.00
Namibia 1996 -5.88 0.97 0.96 3.00
Namibia 1997 6.44 0.97 0.96 4.00
Namibia 1998 5.03 1.06 1.96 5.00
Namibia 1999 1.59 1.06 1.96 4.00
Namibia 2000 1.93 1.07 0.96 2.00
Namibia 2001 1.54 1.07 2.96 3.00
Namibia 2002 8.73 1.15 1.96 1.00
Namibia 2003 5.47 1.16 4.96 5.00
Namibia 2004 4.30 1.19 2.96 6.00
Namibia 2005 0.86 1.20 3.96 5.00
Namibia 2006 15.01 1.37 1.96 4.00
Namibia 2007 8.48 1.47 2.96 6.00
Namibia 2008 3.90 1.47 4.96 4.00
Namibia 2009 -15.88 1.67 3.96 5.00
Namibia 2010 12.08 1.67 1.96 4.00
Namibia 2011 2.44 2.00 5.96 4.00
Namibia 2012 7.46 2.00 4.96 5.00
Namibia 2013 9.30 2.45 6.96 4.00
Namibia 2014 4.39 2.45 1.96 6.00
Namibia 2015 2.99 2.48 3.96 4.00
Namibia 2016 -7.11 2.48 3.99 7.00

The ggplot function call needs to be to one dataframe, whereas you have both gapminder and Africa. You should join these first.

1 Like

How do i join gapminder and Africa?

Joins via dplyr:

2 Likes