R Language-Animation

Source: Internet
Author: User
Tags imagemagick

Use animations to make graphics more image and reflect data changes

1. Installation Environment Gganimate

1 if(!require (Devtools)) Install.packages ("Devtools")2Devtools::install_github ("dgrtwo/gganimate")3 #It is important to note that this package relies on ImageMagick to produce animations, so you need to install ImageMagick4Install.packages ("Installr")5Installr::install. ImageMagick ("http://www.imagemagick.org/script/download.php")6 #In the process of installing the software, there is a page all ticked, otherwise the animation cannot find the executable file

2. Loading the package

1 Library (READR) 2 Library (DPLYR) 3 Library (DT) 4 Library (MAPS) 5 Library (GGPLOT2) 6 Library (ggthemes) 7 Library (tibble) 8 Library (lubridate) 9 Library (Tidyr) Ten Library (gganimate)

3. Load Data Set

1 ' Https://raw.githubusercontent.com/d4tagirl/R-Ladies-growth-maps/master/rladies.csv ' 2 rladies <-read_csv (URL (url_csv))%>%3   Select ( -1)4 DataTable ( Rladies, rownames = FALSE,5           options = List (Pagelength = 5))

Conclusion: This data set is a Rladies dataset and is a community of global feminist organizations that mainly change gender discrimination

Primary field: Screen_name community Name


Created_at creation Time

Number of follower followers

Age_days to Present time (2017-05-16)

Longitude of the Lon community

Latitude of the LAT community

4. Static graphics

1 #load the world map2World <-Ggplot () +3Borders' World', color='gray85', fill='gray80')+4 Theme_map ()5 6 #load each community as a point onto the map7Map <-World +8Geom_point (Aes (x=lon,y=lat,size=followers), data=rladies,colour='Purple', alpha=.5) +9Scale_size_continuous (Range=c (1,8), Breaks=c (250,500,750,1000)) +TenLabs (size='followers')

Conclusion: It can be seen that the feminist communities in the United States and Europe are

5. Create dynamic graphics (show changes every one months)

#use Tibble to create start date and start point data, Tribble is an alternative to Data.frameGhost_points_ini <-tibble (Created_at= As. Date ('2011-09-11'), followers=0,lon=0,lat=0)#use tibble to create end date and end point dataGhost_points_fin <-tibble (Created_at=seq (AS. Date ('2017-05-16'), as. Date ('2017-05-30'), by=' Days'), followers=0,lon=0,lat=0)#gganimate need to specify frame as a parameter,#Cumulative for T means map <-World +Geom_point (AES (x=lon,y=lat,size=followers, Frame=created_at,cumulative=T), Data=rladies,color='Purple', alpha=.5) +Geom_point (AES (x=lon,y=lat,size=followers, Frame=created_at,cumulative=T), Data=ghost_points_ini,color='Purple', alpha=0) +Geom_point (AES (x=lon,y=lat,size=followers, Frame=created_at,cumulativte=T), Data=ghost_points_fin,color='Purple', alpha=0) +scale_size_continuous (Range=c (1,8), Breaks=c (250,500,750,1000)) +Labs (Size='followers')#Turn on animationGganimate (map)

Conclusion: It can be known that the feminist community originated in the United States, slowly spread to Europe, and finally to Latin America, this map has a modified space, the dot with the time of the establishment of the Community increased

6. Modifying dynamic graphics

1 #Select the date displayed in the month display (1,10,20)2Dates <-as_tibble (seq (Floor_date (AS. Date (min (rladies$created_at)),3Unit ='Month'),4As. Date ('2017-05-15'),5by=' Days'))%>%6Filter (value)%inch% c (1,10,20))7 8 #Create a new dataset, select only Screen_name and date, and calculate the user's scale based on time9Rladies_frames <-rladies%>%TenSelect (Screen_name)%>% OneExpand (Screen_name,date=dates$value)%>% ARight_join (rladies,by='Screen_name')%>% -Filter (Date > Created_at)%>% -Mutate (Age_total=as.numeric (age_days,units=' Days'), theAge_at_date= as.numeric (difftime (date,created_at,units =' Days'), -Units =' Days'), -Est_followers= ((followers-1)/age_total) *age_at_date) -  +Ghost_points_ini2 <-Ghost_points_ini%>% -Mutate (date=created_at,est_followers =0) +  AGhost_points_fin2 <-Ghost_points_fin%>% atExpand (date=created_at,rladies)%>% -Select (date,est_followers=Followers,lon,lat) -  -Map_frames <-World + -Geom_point (Aes (x=lon,y=lat,size=est_followers,frame=date), -Data=rladies_frames,colour='Purple', alpha=.5) + inGeom_point (Aes (x=lon,y=lat,size=est_followers,frame=date), -data = ghost_points_ini2,alpha=0) + toGeom_point (Aes (x=lon,y=lat,size=est_followers,frame=date), +data = ghost_points_fin2,colour='Purple', alpha=.5) + -Scale_size_continuous (Range=c (1,8), Breaks=c (250,500,750,1000)) + theLabs (size='followers') *  $Gganimate (Map_frames)

Conclusion: It can be seen that the global feminist community continues to grow in the process

The 2012-2016-year feminist community is in its infancy, concentrated in the United States,

2016-2017 years of development in Europe, the 2017 European feminist community is larger than the United States

2017 years later, feminist community King Latin America began to develop gradually

R Language-Animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.