Webscraping and Visualizing the Top CryptoCurrencies
Top Cryptocurrencies According to Price and Market Cap
Webscraping
Data Visualization
Author
Olamide Adu
Published
June 7, 2024
Cryptocurrencies have captivated the financial world, bringing immense joy to some and heartache to many. One thing is certain: when you get it right and strike gold with cryptocurrencies, it can set you up for life (depending on your frugality and investment amount). As a data scientist, I know the thrill of diving into this dynamic market.
This post, however, isn’t about trading strategies or price forecasting. Instead, it’s a exploring the capabilities of R in web scraping using the rvest package. Since I like to spend time in creating good visuals, I will be making some interesting visualization here.
Getting Our data
First, I scraped data from CoinMarketCap using the URL https://coinmarketcap.com/all/views/all/. The code extracts a specific table and selects relevant columns like name, symbol, market cap, and price.
I started by cleaning the column names using janitor::clean_names() and selecting the columns I needed. Then, I ensured data types were appropriate by converting market_cap and price to numeric values.
Is the data structure as expected? From the data which we have above, there are some columns that needs their data types changed. The market_cap and price column should be numeric/double data type and not character.
Now, let’s visualize the data! I created a donut chart to represent the market cap distribution of the top six cryptocurrencies. The remaining currencies are grouped into an “Other” category.
The code calculates the market cap share for each currency, along with cumulative values and labels for the chart.
Show the code
crypto <- crypto |>mutate(new_sym =fct_lump( symbol, n =6, w = market_cap ) )crypto |>summarize(.by = new_sym,market_cap =sum(market_cap),count =n() )
Table 1: Top Six Cryptocurrencies according to Market Capitalization
# A tibble: 7 × 3
new_sym market_cap count
<fct> <dbl> <int>
1 BTC 2362585749349 1
2 ETH 467356434607 1
3 XRP 187503948068 1
4 USDT 163741940923 1
5 BNB 112508144016 1
6 SOL 97921898217 1
7 Other 268953092294 14
Table 1 shows that the market cap has been compressed into 7, 6 for the top cryptocurrency and 14 lumped together into a new category, Other.
As shown in Figure 1 Bitcoin, and ETH are clearly dominating the crypto space in market capitalization. Assets such as BNB, SOL and USDT are slowly increasing their dominance ranging from 3 - 6%.
Top 20 Cryptocurrencies Price
{.justify} I downloaded logos for the top 20 cryptocurrencies and added them as an “images” column to the data frame.
The code then creates a bar chart to visualize individual cryptocurrency prices, with labels indicating the price for each currency.
crypto_img |>ggplot(aes(price, fct_reorder(images, price))) +geom_col(width = .1,fill ="#FBD25B" ) +geom_label(aes(label =round(price, 2)),col ="white",fill ="#AE1D0E",size =2.5 ) +labs(title ="Price of the Top 20 Cryptocurrencies" ) +scale_x_log10(label = scales::label_number()) +theme_minimal() +theme(axis.text.y = ggtext::element_markdown(),axis.text.x =element_blank(),axis.ticks =element_blank(),axis.title =element_blank(),plot.title =element_text(hjust = .5, color ="#AE1D0E") )
Conclusion
In this project, I successfully scraped cryptocurrency data, cleaned it for analysis, and created visualizations to explore market cap distribution and individual cryptocurrency prices. This process demonstrates the power of web scraping and data visualization in R. Interesting thing is, whenever this page is reloaded, you will always have the most recent price, but do not trust the visuals as the logo may get displaced already. I should develop a shiny app for this instead