library(tidyverse)
library(readxl)
library(gganimate)
library(scales)
Apple <- read_excel("Apple.xlsx", sheet = "Sheet1 (3)")
# View(Apple)
Apple <- Apple %>%
arrange(Year, desc(Revenue))%>%
group_by(Year) %>%
mutate(Rank=row_number()) %>%
mutate(Product = str_replace(string = Product, pattern = "Other Music Related Products and Services", replacement = "Other Music Services"))
my_theme <- theme_classic(base_family = "Times") +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(axis.line.y = element_blank()) +
theme(legend.background = element_rect(fill = "gainsboro")) +
theme(plot.background = element_rect(fill = "gainsboro")) +
theme(panel.background = element_rect(fill = "gainsboro"))
Apple %>%
ggplot() +
aes(xmin = 0 ,
xmax = Revenue
) +
aes(
ymin = Rank - .45,
ymax = Rank + .45,
y = Rank) +
facet_wrap(~ Year) +
geom_rect(alpha = .7) +
aes(fill = Product) +
scale_fill_viridis_d(option = "magma",
direction = -1) +
scale_x_continuous(
limits = c(-90000, 200000),
labels = unit_format(unit = "B", scale = 1e-3),
breaks = c(0, 50000, 100000, 150000, 200000)
#breaks = c(0, 400, 800, 1200)
) +
geom_text(col = "gray13",
hjust = "right",
aes(label = Product),
x = -8000) +
scale_y_reverse() +
labs(fill = NULL) +
labs(x = 'Revenue (Billions)') +
labs(y = "") +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(axis.line.y = element_blank()) -> myplot
# my_theme
#+
# my_theme
my.animation <-
myplot +
facet_null() +
#scale_x_continuous(
#limits = c(-150000, 200000),
#breaks = c(0, 400, 800, 1200)
#) +
geom_text(x = 100000 , y = -7,
family = "Times",
aes(label = as.character(Year)),
size = 25, col = "grey18") +
aes(group = Product) +
labs(title = "Apple's revenue from different sources by year") +
gganimate::transition_time(Year)
animate(my.animation, height = 5,
width = 10, units = "in", res = 350,
nframes = 200, fps = 20)

anim_save("C:/Users/User/Desktop/gganimate/gapminder_example.gif")