Library
library(mongolite)
## Warning: package 'mongolite' was built under R version 4.4.3
library(rvest)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(httr)
library(ggplot2)
Scraping Data
# URL Wikipedia
url <- "https://en.wikipedia.org/wiki/List_of_largest_companies_by_revenue"
# Baca halaman
page <- read_html(url)
# Ambil semua tabel
tables <- page %>% html_nodes("table")
# Ambil tabel pertama
companies_table <- tables[[1]] %>% html_table(fill = TRUE)
# Perbaiki nama kolom
colnames(companies_table) <- make.names(colnames(companies_table))
# Hapus baris pertama (karena itu header ganda)
companies_table <- companies_table %>% slice(-1)
# Hapus kolom State.owned dan Ref.
companies_table <- companies_table %>%
select(-State.owned, -Ref.)
# Bersihkan dan konversi tipe data
companies_table <- companies_table %>%
filter(!is.na(Rank)) %>%
mutate(
Revenue = as.numeric(gsub("[\\$,]", "", Revenue)),
Profit = as.numeric(gsub("[\\$,]", "", Profit)),
Employees = as.integer(gsub(",", "", Employees))
)
# Ganti nama kolom Headquarters.note.1. menjadi Headquarters
colnames(companies_table)[colnames(companies_table) == "Headquarters.note.1."] <- "Headquarters"
# Lihat hasil akhir
glimpse(companies_table)
## Rows: 50
## Columns: 7
## $ Rank <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", …
## $ Name <chr> "Walmart", "Amazon", "State Grid Corporation of China", "…
## $ Industry <chr> "Retail", "Retailinformation technology", "Electricity", …
## $ Revenue <dbl> 680985, 637959, 545948, 480446, 429700, 476000, 400278, 3…
## $ Profit <dbl> 19436, 59248, 9204, 106246, 9393, 25250, 14405, 93736, 88…
## $ Employees <int> 2100000, 1556000, 1361423, 73311, 513434, 1026301, 400000…
## $ Headquarters <chr> "United States", "United States", "China", "Saudi Arabia"…
Koneksi ke MongoDB
library(mongolite)
library(dplyr)
# URL MongoDB Atlas
url <- "mongodb+srv://muhsunan:biRploUXILrwrchf@cluster0.52tmris.mongodb.net/"
# Koneksi ke MongoDB Atlas
companies <- mongo(collection = "Companies", db = "Company_Data", url = url)
# Hapus data lama dan masukkan data baru
companies$remove("{}")
companies$insert(companies_table)
## List of 5
## $ nInserted : num 50
## $ nMatched : num 0
## $ nRemoved : num 0
## $ nUpserted : num 0
## $ writeErrors: list()
# Ambil data dan konversi ke data frame
companies_df <- as.data.frame(companies$find())
# Ubah tipe data: chr ke factor, int/dbl ke numeric dan Rank ke numeric
companies_df[] <- lapply(companies_df, function(x) {
if (is.character(x)) as.factor(x) else if (is.integer(x) || is.double(x)) as.numeric(x) else x
})
# Ubah kolom Rank kembali jadi numeric jika perlu
companies_df$Rank <- as.numeric(as.character(companies_df$Rank))
# Tampilkan struktur data frame yang sudah dimodifikasi
str(companies_df)
## 'data.frame': 50 obs. of 7 variables:
## $ Rank : num 1 2 3 4 5 6 7 8 9 10 ...
## $ Name : Factor w/ 50 levels "Agricultural Bank of China",..: 50 3 41 38 17 16 47 4 8 24 ...
## $ Industry : Factor w/ 14 levels "Automotive","Commodities",..: 12 13 5 11 11 11 9 10 8 9 ...
## $ Revenue : num 680985 637959 545948 480446 429700 ...
## $ Profit : num 19436 59248 9204 106246 9393 ...
## $ Employees : num 2100000 1556000 1361423 73311 513434 ...
## $ Headquarters: Factor w/ 14 levels "China","France",..: 14 14 1 8 1 1 14 14 14 14 ...
Visualisasi
# Visualisasi Pendapatan Perusahaan Terbesar tanpa menampilkan legenda Company
ggplot(companies_df[1:10, ], aes(x = reorder(Name, Revenue), y = Revenue, fill = Name)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(
title = "Top 10 Largest Companies by Revenue",
x = "Company",
y = "Revenue (in billions)"
) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
guides(fill = "none") # Menghilangkan legenda Company

# Visualisasi Pendapatan Perusahaan Terbesar tanpa menampilkan legenda Company
ggplot(companies_df[1:10, ], aes(x = reorder(Name, Revenue), y = Revenue, fill = Name)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(
title = "Top 10 Largest Companies by Revenue",
x = "Company",
y = "Revenue (in billions)"
) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
guides(fill = "none") # Menghilangkan legenda Company

# Visualisasi Jumlah Karyawan Perusahaan Terbesar
ggplot(companies_df[1:10, ], aes(x = reorder(Name, Employees), y = Employees, fill = Name)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(
title = "Top 10 Companies by Employees",
x = "Company",
y = "Employees",
fill = "Company"
) +
theme_minimal() +
scale_y_continuous(labels = scales::comma) +
guides(fill = "none") # Menghilangkan legenda Company

# Visualisasi Distribusi Perusahaan Berdasarkan Negara
country_distribution <- companies_df %>%
count(Headquarters) %>%
arrange(desc(n))
# Pie Chart
ggplot(country_distribution, aes(x = "", y = n, fill = Headquarters)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
labs(title = "Distribution of Top Companies by Country") +
theme_void()

# Visualisasi Korelasi Pendapatan dan Laba
ggplot(companies_df, aes(x = Revenue, y = Profit)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(
title = "Correlation Between Revenue and Profit",
x = "Revenue (in billions)",
y = "Profit (in billions)"
) +
scale_x_continuous(labels = scales::comma) +
scale_y_continuous(labels = scales::comma) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
