Selamat datang di Rmd Data Visualization saya. Data yang saya gunakan adalah data “Loan Kiva 2015”. Kiva adalah sebuah organisasi non-profit yang membantu masyarakat untuk meminjam uang dengan model P2P (peer-to-peer).Beberapa Visualisasi yang akan di tampilkan adalah -. 10 besar Aktivitas di Mexico dengan rata-rata pinjaman terbesar -. Funding duration trend di Mexico
Set up library yang akan kita gunakan
library(lubridate)
library(ggplot2)Input data ke R dengan nama objek loan
#input data yang akan kita gunakan
loan <- read.csv("loan_kiva.csv")
tail(loan)Inspect Data
#cek dimensi data
dim(loan)## [1] 165040 14
#cek data teratas
head(loan)#cek struktur data
str(loan)## 'data.frame': 165040 obs. of 14 variables:
## $ id : int 823048 823138 823051 823143 823154 823117 823100 823132 823137 823050 ...
## $ funded_amount : int 450 200 175 675 1450 250 450 900 225 250 ...
## $ loan_amount : int 450 200 175 675 1450 250 450 900 225 250 ...
## $ activity : chr "Butcher Shop" "Food Production/Sales" "Fruits & Vegetables" "Fishing" ...
## $ sector : chr "Food" "Food" "Food" "Food" ...
## $ country : chr "Philippines" "Philippines" "Philippines" "Philippines" ...
## $ region : chr "Kabankalan, Negros Occidental" "Baybay, Leyte" "Kabankalan, Negros Occidental" "Carles, Iloilo" ...
## $ currency : chr "PHP" "PHP" "PHP" "PHP" ...
## $ partner_id : int 145 125 145 125 202 145 245 125 125 145 ...
## $ posted_time : chr "2015-01-01 02:38:53" "2015-01-01 15:57:25" "2015-01-01 02:49:11" "2015-01-01 16:20:43" ...
## $ funded_time : chr "2015-01-01 16:20:50" "2015-01-05 00:21:31" "2015-01-01 06:12:18" "2015-01-05 19:21:50" ...
## $ term_in_months : int 8 10 8 20 11 8 14 12 12 5 ...
## $ lender_count : int 14 5 4 26 45 7 16 19 7 7 ...
## $ repayment_interval: chr "irregular" "irregular" "irregular" "irregular" ...
#Cek Missing data
anyNA(loan)## [1] FALSE
#Cek kolom posted_time sebelum dikonversi
class(loan$posted_time)## [1] "character"
#Ubah posted_time ke tipe data yang tepat
ymd_hms(head(loan$posted_time))## [1] "2015-01-01 02:38:53 UTC" "2015-01-01 15:57:25 UTC"
## [3] "2015-01-01 02:49:11 UTC" "2015-01-01 16:20:43 UTC"
## [5] "2015-01-01 19:59:25 UTC" "2015-01-01 14:14:57 UTC"
loan$posted_time <- ymd_hms(loan$posted_time)
#Ubah funded_time
loan$funded_time <- ymd_hms(loan$funded_time)
#Cek data apakah sudah tepat
str(loan)## 'data.frame': 165040 obs. of 14 variables:
## $ id : int 823048 823138 823051 823143 823154 823117 823100 823132 823137 823050 ...
## $ funded_amount : int 450 200 175 675 1450 250 450 900 225 250 ...
## $ loan_amount : int 450 200 175 675 1450 250 450 900 225 250 ...
## $ activity : chr "Butcher Shop" "Food Production/Sales" "Fruits & Vegetables" "Fishing" ...
## $ sector : chr "Food" "Food" "Food" "Food" ...
## $ country : chr "Philippines" "Philippines" "Philippines" "Philippines" ...
## $ region : chr "Kabankalan, Negros Occidental" "Baybay, Leyte" "Kabankalan, Negros Occidental" "Carles, Iloilo" ...
## $ currency : chr "PHP" "PHP" "PHP" "PHP" ...
## $ partner_id : int 145 125 145 125 202 145 245 125 125 145 ...
## $ posted_time : POSIXct, format: "2015-01-01 02:38:53" "2015-01-01 15:57:25" ...
## $ funded_time : POSIXct, format: "2015-01-01 16:20:50" "2015-01-05 00:21:31" ...
## $ term_in_months : int 8 10 8 20 11 8 14 12 12 5 ...
## $ lender_count : int 14 5 4 26 45 7 16 19 7 7 ...
## $ repayment_interval: chr "irregular" "irregular" "irregular" "irregular" ...
Selanjutnya membuat data frame mexico yang berisi data berdasarkan country Mexico
mexico <- loan[loan$country %in% c("Mexico"),]
head(mexico)Membuat kolom funding_duration yang berisi selisih waktu antara funded_time dan posted_time
#kolom funding_duration
mexico$funding_duration <- mexico$funded_time - mexico$posted_time
str(mexico$funding_duration)## 'difftime' num [1:1820] 22710.7166666667 3319.01666666667 273.45 198.483333333333 ...
## - attr(*, "units")= chr "mins"
#ubah tipe data menjadi numeric
mexico$funding_duration <- as.numeric(mexico$funding_duration)Membuat kolom month yang berisi nama bulan dari posted time
mexico$month <- month(mexico$posted_time, label = T)
head(mexico)Sekarang kita mulai melakukan agregasi data untuk mendapatkan rata-rata (mean) dari durasi waktu pembiayaan (funding duration) per bulannya (month)tiap jangka waktu pengembalian (repayment interval)
#agregasi data
loan_agg <- aggregate(funding_duration ~ month + repayment_interval,
data = mexico, FUN = mean)
head(loan_agg)Selanjutnya kita melakukan agregasi data untuk mendapatkan rata-rata(mean) dari jumlah pinjaman(loan_amount)per sector
#agregasi data
loan_agg2 <- aggregate(loan_amount ~ sector + country ,
data = mexico, FUN = mean)
head(loan_agg2)#Mengurutkan data dan ambil top 10
loan_urut <- loan_agg2[order(loan_agg2$loan_amount, decreasing = T), ]
loan_viz <- head(loan_urut, 10)
head(loan_viz)Sekarang kita akan melakukan plotting berdasarkan kasus diatas
#plotting
ggplot(data = loan_viz,mapping = aes(x = loan_amount,
y = reorder(sector,loan_amount))) +
geom_col(aes(fill = loan_amount)) +
# untuk menambahkan label
geom_text(aes(label = round(loan_amount,2), x= loan_amount/2),size = 3)+
# untuk custom label pada x,y,title,etc
labs(x = "Loan Amount",
y = NULL,
title = "Top 10 Activity on Mexico",
fill = "Freq")+
scale_fill_gradient(low = "seagreen1", high = "skyblue") +
theme_gray()+
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5),
plot.background = element_rect(fill = "snow2"))Grafik diatas menunjukan 10 sektor teratas di negara Mexico berdasarkan tingkat peminjaman. Dapat dilihat sektor Education berada ditingkat teratas dengan 3235 rata-rata pinjaman diikuti dengan Arts dengan 3111.25 rata-rata pinjaman. Sedangkan sektor Entertainment berada ditempat terendah dengan 1803.33 rata-rata pinjaman.
ggplot(loan_agg, aes(x = month , y = funding_duration, color = repayment_interval , group = repayment_interval))+
geom_line()+
geom_point()+
geom_text(aes(label = round(funding_duration,2)), size = 3,position = position_dodge(width = 1) ) +
labs(title = "Funding Duration Trend on Mexico, 2015",
x = NULL, y = "Funding Duration",
color = "Repayment Interval")+
theme_gray()+
theme(legend.position = "top",
plot.title = element_text(hjust = 0.5),
plot.background = element_rect(fill = "gray"),
legend.background = element_rect(fill = "snow3"))Dari plot diatas dapat kita lihat monthly repayment interval memiliki waktu pembiayaan tertinggi pada tahun 2015 tepatnya pada bulan October. Untuk waktu pembiayaan tercepat pada tahun 2015 terdapat bullet repayment interval di bulan februari.