Last active
April 20, 2022 14:50
-
-
Save srvanderplas/91a05bf4ae79e2c99d499e521e7b231d to your computer and use it in GitHub Desktop.
Plotting with images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
library(stringr) # string manipulation | |
library(readr) # parse_number | |
library(magrittr) # pipe and set_names function | |
if(!"ggimage" %in% installed.packages()) { | |
install.packages("ggimage") | |
} | |
library(ggimage) | |
if(!"rvest" %in% installed.packages()) { | |
install.packages("rvest") | |
} | |
# Read in pokemon data | |
library(rvest) | |
pokemon_data <- read_html("https://pokemondb.net/pokedex/all") %>% | |
html_table() %>% | |
extract2(1) | |
pokemon_data %>% | |
filter(row_number() <= 151) %>% | |
sample_n(20) %>% | |
ggplot(aes(x = Total, y = HP, image = tolower(Name))) + geom_pokemon() | |
## Data Science Popularity | |
page <- read_html("https://www.tiobe.com/tiobe-index/") | |
# Get image URLs from the table separately | |
images <- html_nodes(page, ".td-top20 img") %>% | |
html_attrs() %>% | |
bind_rows() %>% | |
set_names(c("URL", "Language", "style")) %>% | |
select(-style) %>% | |
mutate(URL = paste0("https://www.tiobe.com/tiobe-index/", URL), | |
Language = str_remove_all(Language, " page")) | |
# Read data from the table of top-20 languages | |
data_science_language <- page %>% | |
html_table() %>% | |
extract2(1) %>% | |
select(-c(3:4)) %>% | |
# Add columns with images | |
left_join(images, by = c(`Programming Language` = "Language")) %>% | |
# Sort by ratings | |
mutate(Ratings = parse_number(Ratings)) %>% | |
arrange(desc(Ratings)) %>% | |
# Set order of labels | |
mutate(Language = factor(`Programming Language`, levels = `Programming Language`, ordered = T)) | |
# Create the plot | |
ggplot(data_science_language, aes(x = Language, y = Ratings)) + | |
geom_bar(stat = "identity") + | |
coord_flip() + | |
geom_image(aes(image = URL)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://i.imgur.com/9bHD4ix.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment