fert<-read_csv("https://raw.githubusercontent.com/kitadasmalley/FA2020_DataViz/main/data/gapminderFert.csv")

head(fert)
## # A tibble: 6 x 6
##   Country      year  life  fert   pop continent
##   <chr>       <dbl> <dbl> <dbl> <dbl> <chr>    
## 1 Afghanistan  1962  33.0  7.67   9.3 Asia     
## 2 Afghanistan  1963  33.5  7.67   9.5 Asia     
## 3 Afghanistan  1964  34.1  7.67   9.7 Asia     
## 4 Afghanistan  1965  34.6  7.67   9.9 Asia     
## 5 Afghanistan  1966  35.1  7.67  10.1 Asia     
## 6 Afghanistan  1967  35.7  7.67  10.4 Asia
p<- fert%>%
  filter(year == 2015)%>% 
  ggplot(aes(fert, life, size = pop, color = continent)) +
  labs(x="Fertility Rate", y = "Life expectancy at birth (years)", 
       caption = "(Based on data from Hans Rosling - gapminder.com)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) +
  geom_point() 

p

2) Add some interaction: plotly

The plotly package can be used to created interactive graphics. Although it is an entirely different paradigm of programming graphics, it does have a very nice function, ggplotly that can be added on to ggplot objects.

Use the tooltip to learn more about each point

p<- fert%>%
  ggplot(aes(x=fert, y=life, size = pop, color = continent,frame = year)) +
  labs(x="Fertility Rate", y = "Life expectancy at birth (years)", 
       caption = "(Based on data from Hans Rosling - gapminder.com)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) +
  geom_point(aes(text=Country))

ggplotly(p)

3) Now let’s animate! gganimate

#install.packages("gganimate")
#install.packages("gifski")


p1 <- ggplot(fert, aes(fert, life, size = pop, color = continent, frame = year)) +
  labs(x="Fertility Rate", y = "Life expectancy at birth (years)", 
       caption = "(Based on data from Hans Rosling - gapminder.com)", 
       color = 'Continent',size = "Population (millions)") + 
  ylim(30,100) + # To prevent the y-axis from changing over time 
  geom_point() +
  #scale_color_brewer(type = 'div', palette = 'Spectral') + 
  # gganimate code
  ggtitle("Year: {frame_time}") +
  transition_time(year) +
  ease_aes("linear") +
  enter_fade() + #to avoid flashing points
  exit_fade()

animate(p1,fps = 4, width = 600, height = 400, renderer = gifski_renderer()) 

anim_save("output.gif")

Notice that this animation is a little choppy, because there is one observation every year. You can use the tweener package to interpolate and make smoother transitions https://www.r-bloggers.com/2017/05/create-smooth-animations-in-r-with-the-tweenr-package/.

More Demos

1: Area Graph

# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv", header=T)

str(data)
## 'data.frame':    1822 obs. of  2 variables:
##  $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01" ...
##  $ value: num  136 147 147 140 126 ...
data$date <- as.Date(data$date)

# Usual area chart
p <- data %>%
  ggplot( aes(x=date, y=value)) +
  geom_area(fill="#69b3a2", alpha=0.5) +
  geom_line(color="#69b3a2") +
  ylab("bitcoin price ($)") 

# Turn it interactive with ggplotly
p <- ggplotly(p)
p

II. Example with heatmaply

Here is an example showing 8 general features like population or life expectancy for about 30 countries in 2015. Data come from the French National Institute of Demographic Studies https://www.ined.fr/en/everything_about_population/data/all-countries/?lst_continent=908&lst_pays=926

Heatmap (left to right): * Pop * Birth rate * Mortality rate * Life expectancy * Infant Mortality * Children per woman * Growth rate * Population aged 65

library(viridis)

#install.packages("heatmaply")
library(heatmaply)

# Load data 
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/multivariate.csv", header=T, sep=";")
head(data)
##           Country  Pop Birth.rate Mortality.rate Life.expectancy
## 1 Channel Islands  166      9.280          9.140          81.269
## 2         Denmark 5733     10.707          9.620          80.839
## 3         Estonia 1302     10.698         12.657          77.342
## 4         Finland 5556     10.672          9.865          81.576
## 5         Iceland  337     12.836          6.467          83.220
## 6         Ireland 4791     13.179          6.562          81.621
##   Infant.mortality Children.per.woman Growth.rate Population.aged.65. Group
## 1            7.276              1.492        4.56                  30 North
## 2            3.495              1.758        3.67                1132 North
## 3            2.843              1.657       -2.63                 256 North
## 4            1.839              1.764        2.65                1212 North
## 5            1.752              1.893        7.83                  50 North
## 6            2.385              1.998        8.84                 682 North
##   Continent
## 1    Europe
## 2    Europe
## 3    Europe
## 4    Europe
## 5    Europe
## 6    Europe
colnames(data) <- gsub("\\.", " ", colnames(data)) # gsub, looking for anything that have a , and replace it with space

# Select a few country

data <- data %>% 
  filter(Country %in% c("France", "Sweden", "Italy", "Spain", "England", "Portugal", "Greece", "Peru", "Chile", "Brazil", "Argentina", "Bolivia", "Venezuela", "Australia", "New Zealand", "Fiji", "China", "India", "Thailand", "Afghanistan", "Bangladesh", "United States of America", "Canada", "Burundi", "Angola", "Kenya", "Togo")) %>%
  arrange(Country) %>%
  mutate(Country = factor(Country, Country))

# Matrix format
mat <- data
rownames(mat) <- mat[,1]
mat <- mat %>% dplyr::select(-Country, -Group, -Continent)
mat <- as.matrix(mat)

head(mat)
##                Pop Birth rate Mortality rate Life expectancy Infant mortality
## Afghanistan  34936       31.0            7.6            61.7             62.8
## Angola       27498       43.6           12.6            53.9             86.8
## Argentina    44692       16.8            7.5            77.0             11.5
## Australia    24967       12.9            6.7            83.1              3.3
## Bangladesh  166735       18.4            5.2            73.1             24.8
## Bolivia      11218       22.8            7.2            69.8             34.3
##             Children per woman Growth rate Population aged 65 
## Afghanistan               4.15        21.8                 906
## Angola                    5.75        31.0                 658
## Argentina                 2.26         9.3                 507
## Australia                 1.86        12.9                3945
## Bangladesh                2.06        11.4                8373
## Bolivia                   2.81        14.7                 767
# Heatmap

p <- heatmaply(mat, 
               dendrogram = "none",
               xlab = "", ylab = "", 
               main = "",
               scale = "column",
               margins = c(60,100,40,20),
               grid_color = "white",
               grid_width = 0.00001,
               titleX = FALSE,
               hide_colorbar = TRUE,
               branches_lwd = 0.1,
               label_names = c("Country", "Feature:", "Value"),
               fontsize_row = 5, fontsize_col = 5,
               labCol = colnames(mat),
               labRow = rownames(mat),
               heatmap_layers = theme(axis.line=element_blank())
)
p