Visualisasi Aliran & Jaringan

Visualisasi Aliran

Berikut ini adalah ilustrasi yang diperoleh dari

library(tidyverse)
flight<-read_csv("https://raw.githubusercontent.com/raoy/data/master/flight.csv")
flight
## # A tibble: 11 x 5
##    Route                 Source.lat Source.lon Dest.lat Dest.lon
##    <chr>                      <dbl>      <dbl>    <dbl>    <dbl>
##  1 Los Angeles-Seattle         34.5     -118.      47.6   -122. 
##  2 Los Angeles-Houston         34.5     -118.      29.8    -95.4
##  3 Los Angeles-Salt Lake       34.5     -118.      40.8   -112. 
##  4 Los Angeles-Denver          34.5     -118.      39.7   -105. 
##  5 Los Angeles-Chicago         34.5     -118.      41.9    -87.6
##  6 Los Angeles-Phoenix         34.5     -118.      33.4   -112. 
##  7 Atlanta-New York            33.8      -84.4     40.7    -74.0
##  8 Atlanta-Pittsburgh          33.8      -84.4     40.4    -80  
##  9 Atlanta-Newark              33.8      -84.4     40.7    -74.2
## 10 Atlanta-Boston              33.8      -84.4     42.4    -71.1
## 11 Atlanta-Minneapolis         33.8      -84.4     45.0    -93.3
#other map arguments include "world", "usa" and "county"
usMap <- borders("state", colour="grey", fill="white")
ggplot() + usMap

flightplot <- ggplot() + usMap +
  geom_curve(data=flight,
             aes(x=Source.lon, y=Source.lat, xend=Dest.lon, yend=Dest.lat),
             col="#00008b",
             size=.5,
             curvature=0.2) +
  geom_point(data=flight,
             aes(x=Source.lon, y=Source.lat), 
             colour="blue",
             size=1.5) +
  geom_point(data=flight,
             aes(x=Dest.lon, y=Dest.lat), 
             colour="blue") +
  theme(axis.line=element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.ticks=element_blank(),
        plot.title=element_text(hjust=0.5, size=12)) +
  ggtitle("Flight Map")
flightplot

Visualisasi Jaringan

library(GGally)
library(network)
library(sna)

# random graph
net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)

# vertex names
network.vertex.names(net) = letters[1:10]

ggnet2(net)

Untuk mempelajari lebih lanjut mengenai visualisasi ini, dapat dipelajari pada Briatte (2019).

Sebagai ilustrasi lainnya, kita akan membangkitkan data dari sebaran binom seperti di bawah ini.

m <- matrix(rbinom(100,1,.4),10,10)
diag(m) <- 0
g <- network(m, directed=FALSE)
summary(g)
## Network attributes:
##   vertices = 10
##   directed = FALSE
##   hyper = FALSE
##   loops = FALSE
##   multiple = FALSE
##   bipartite = FALSE
##  total edges = 27 
##    missing edges = 0 
##    non-missing edges = 27 
##  density = 0.6 
## 
## Vertex attributes:
##   vertex.names:
##    character valued attribute
##    10 valid vertex names
## 
## No edge attributes
## 
## Network adjacency matrix:
##    1 2 3 4 5 6 7 8 9 10
## 1  0 0 0 1 1 1 0 1 0  1
## 2  0 0 0 1 1 1 1 1 1  1
## 3  0 0 0 0 1 1 0 0 1  1
## 4  1 1 0 0 0 0 1 1 0  0
## 5  1 1 1 0 0 0 0 1 1  0
## 6  1 1 1 0 0 0 1 1 0  0
## 7  0 1 0 1 0 1 0 0 1  1
## 8  1 1 0 1 1 1 0 0 1  1
## 9  0 1 1 0 1 0 1 1 0  1
## 10 1 1 1 0 0 0 1 1 1  0

Selanjutnya matriks diubah menjadi objek network.

ggnet2(g, label = TRUE, label.alpha = 0.75)

Ilustrasi: French MPs on Twitter

Ilustrasi berikut ini diambil dari Briatte (2019), menggunakan data yang terdiri dari 339 anggota parlemen dan informasi tentang akun twitter mereka yang saling follow.

# root URL
r = "https://raw.githubusercontent.com/briatte/ggnet/master/"

# read nodes
v = read.csv(paste0(r, "inst/extdata/nodes.tsv"), sep = "\t")
names(v)
## [1] "Sexe"                     "PrÃ.nom"                 
## [3] "Nom"                      "Groupe"                  
## [5] "DÃ.partement.d.Ã.lection" "Num.circonscription"     
## [7] "Commission.permanente"    "Twitter"
# read edges
e = read.csv(paste0(r, "inst/extdata/network.tsv"), sep = "\t")
names(e)
## [1] "Source" "Target"
# network object
net = network(e, directed = TRUE)

# party affiliation
x = data.frame(Twitter = network.vertex.names(net))
x = merge(x, v, by = "Twitter", sort = FALSE)$Groupe
net %v% "party" = as.character(x)

# color palette
y = RColorBrewer::brewer.pal(9, "Set1")[ c(3, 1, 9, 6, 8, 5, 2) ]
names(y) = levels(as.factor(x))

# network plot
ggnet2(net, color = "party", palette = y, alpha = 0.75, size = 4, edge.alpha = 0.5)

Visualisasi Geospasial

Ilustrasi di bawah ini dirujuk dari Data Technik (2019). Data tersedia pada package datasets, berisi 50 observasi dan 9 peubah.

states<-as.data.frame(state.x77)
head(states)
##            Population Income Illiteracy Life Exp Murder HS Grad Frost   Area
## Alabama          3615   3624        2.1    69.05   15.1    41.3    20  50708
## Alaska            365   6315        1.5    69.31   11.3    66.7   152 566432
## Arizona          2212   4530        1.8    70.55    7.8    58.1    15 113417
## Arkansas         2110   3378        1.9    70.66   10.1    39.9    65  51945
## California      21198   5114        1.1    71.71   10.3    62.6    20 156361
## Colorado         2541   4884        0.7    72.06    6.8    63.9   166 103766

Pertama, kita akan coba menampilkan visualisasi data populasi di U.S.

states$region <- tolower(rownames(states))
states_map <- map_data("state")
fact_join <- left_join(states_map, states, by = "region")
ggplot(fact_join, aes(long, lat, group = group))+
  geom_polygon(aes(fill = Population), color = "white")+
  scale_fill_viridis_c(option = "C")+
  theme_classic()

Kita dapat pula mengganti pewarnaan grafik seperti di bawah ini.

ggplot(fact_join, aes(long, lat, group = group))+
  geom_polygon(aes(fill = Income), color = "white")+
  scale_fill_viridis_c(option = "C")+
  theme_classic()

Berikut apabila ingin menampilkan peubah lain, misalnya usia harapan hidup.

fact_join$`Life Exp` <- as.numeric(fact_join$`Life Exp`)
ggplot(fact_join, aes(long, lat, group = group))+
geom_polygon(aes(fill = `Life Exp`), color = "white")+
scale_fill_viridis_c(option = "C")+
theme_classic()

Visualisasi Multivariat : Plot Korespondensi

library(PogromcyDanych)
tab = table(auta2012$Marka,  auta2012$Rodzaj.paliwa)
tab = tab[rowSums(tab) > 300, c(1,2,6)]
library(gplots)


balloonplot(t(tab), 
            main = '', 
            xlab = '',
            ylab = '',
            label = FALSE, 
            show.margins = FALSE)

library(ca)
ca.fit <- ca(tab)
ca.plot <- plot(ca.fit)

Plot korespondensi dapat pula ditampilkan menggunakan ggplot, menggunakan fungsi yang dijelaskan oleh Rcrastinate (2019) berikut ini.

make.ca.plot.df <- function (ca.plot.obj,
                             row.lab = "Rows",
                             col.lab = "Columns") {
  df <- data.frame(Label = c(rownames(ca.plot.obj$rows),
                             rownames(ca.plot.obj$cols)),
                   Dim1 = c(ca.plot.obj$rows[,1], ca.plot.obj$cols[,1]),
                   Dim2 = c(ca.plot.obj$rows[,2], ca.plot.obj$cols[,2]),
                   Variable = c(rep(row.lab, nrow(ca.plot.obj$rows)),
                                rep(col.lab, nrow(ca.plot.obj$cols))))
  rownames(df) <- 1:nrow(df)
  df
}
ca.plot.df <- make.ca.plot.df(ca.plot,
                              row.lab = "Manufacturer",
                              col.lab = "Fuel")
ca.plot.df$Size <- ifelse(ca.plot.df$Variable == "Manufacturer", 2, 1)

ca.plot.df
##                     Label        Dim1         Dim2     Variable Size
## 1               AlfaRomeo -0.11276955 -0.101517432 Manufacturer    2
## 2                    Audi  0.29120210  0.058559213 Manufacturer    2
## 3                     BMW  0.12084709  0.074627184 Manufacturer    2
## 4               Chevrolet -0.79348810 -0.002026356 Manufacturer    2
## 5                Chrysler -0.37626926  0.449452991 Manufacturer    2
## 6                 Citroen  0.15776514 -0.072258801 Manufacturer    2
## 7                   Dacia -0.49854992  0.064652808 Manufacturer    2
## 8                  Daewoo -1.10349094  0.700683682 Manufacturer    2
## 9                   Dodge -0.79296694  0.727442283 Manufacturer    2
## 10                   Fiat -0.44712268  0.094139809 Manufacturer    2
## 11                   Ford  0.14350320 -0.033517359 Manufacturer    2
## 12                  Honda -0.80197745 -0.107891614 Manufacturer    2
## 13                Hyundai -0.32481426 -0.141269268 Manufacturer    2
## 14                 Jaguar -0.54494081 -0.152736541 Manufacturer    2
## 15                   Jeep -0.24231617  0.615451455 Manufacturer    2
## 16                    Kia -0.13071588 -0.078069572 Manufacturer    2
## 17                 Lancia -0.21659958 -0.148368213 Manufacturer    2
## 18              LandRover  0.42410102  0.090955584 Manufacturer    2
## 19                  Lexus -0.82823565 -0.167230915 Manufacturer    2
## 20                  Skoda -0.03952143 -0.054355982 Manufacturer    2
## 21                  Mazda -0.12660612 -0.084311401 Manufacturer    2
## 22          Mercedes-Benz  0.16042381  0.037113303 Manufacturer    2
## 23                   Mini -0.78518561 -0.412141050 Manufacturer    2
## 24             Mitsubishi -0.30755311 -0.105606196 Manufacturer    2
## 25                 Nissan -0.13805025 -0.045816302 Manufacturer    2
## 26                   Opel  0.01767288  0.064296078 Manufacturer    2
## 27                Peugeot  0.17528767 -0.082442991 Manufacturer    2
## 28                Porsche -0.76141615 -0.382389712 Manufacturer    2
## 29                Renault  0.05528161 -0.021484043 Manufacturer    2
## 30                  Rover -0.30657641 -0.044304474 Manufacturer    2
## 31                   Saab -0.16995358 -0.032204461 Manufacturer    2
## 32                   Seat  0.05743904 -0.019509575 Manufacturer    2
## 33                  Smart -0.65591512 -0.417481117 Manufacturer    2
## 34                 Subaru -0.78642633 -0.065129766 Manufacturer    2
## 35                 Suzuki -0.60191511 -0.227358628 Manufacturer    2
## 36                 Toyota -0.03786536 -0.104010734 Manufacturer    2
## 37             Volkswagen  0.24092237  0.020003210 Manufacturer    2
## 38                  Volvo  0.21993182  0.078633443 Manufacturer    2
## 39                benzyna -0.31054437 -0.069150075         Fuel    1
## 40            benzyna+LPG -0.32819850  0.511937021         Fuel    1
## 41 olej napedowy (diesel)  0.24706413  0.001483113         Fuel    1

Untuk membuat plot korespondensi, kita hanya menggunakan 2 dimensi saja, keragaman yang diperoleh pada kedua dimensi tersebut dapat diketahui menggunakan fungsi berikut ini.

ca.sum <- summary(ca.fit)
dim.var.percs <- ca.sum$scree[,"values2"]
dim.var.percs
## [1] 83.50121 16.49879

Berdasarkan objek yang telah dibuat sebelumnya, selanjutnya kita dapat membuat plot korespondensi menggunakan fungsi ggplot().

library(ggplot2)
library(ggrepel)
p <- ggplot(ca.plot.df, aes(x = Dim1, y = Dim2,
                       col = Variable, shape = Variable,
                       label = Label, size = Size)) +
  geom_vline(xintercept = 0, lty = "dashed", alpha = .5) +
  geom_hline(yintercept = 0, lty = "dashed", alpha = .5) +
  geom_point(aes(size=1)) +
  geom_text(aes(label=Label), h=0,v=0,
            size=2.5, color="steelblue")
p

References

Aprilliant, A. (2021, June 15). Introduction to correspondence analysis using R and Indonesia real dataset. Medium. https://towardsdatascience.com/correspondence-analysis-using-r-cd57675ffc3a

Briatte, F. (2019). Ggnet2: Network visualization with ggplot2. briatte.github.io. https://briatte.github.io/ggnet/

[Data Technik]. (2019, December 6). Choropleth map in ggplot2. R-bloggers. https://www.r-bloggers.com/2019/12/choropleth-map-in-ggplot2/

Holtz, Y. (n.d.). How to draw connecting routes on map with R. The R Graph Gallery – Help and inspiration for R charts. https://www.r-graph-gallery.com/how-to-draw-connecting-routes-on-map-with-r-and-great-circles.html

Rcrastinate. (2019, August 19). Correspondence analysis visualization using ggplot. R-bloggers. https://www.r-bloggers.com/2019/08/correspondence-analysis-visualization-using-ggplot/