Assignment 9

library(rvest)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter()         masks stats::filter()
✖ readr::guess_encoding() masks rvest::guess_encoding()
✖ dplyr::lag()            masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
all_books <- list()

for (i in 1:50) {
  if (i == 1) {
    url <- "https://books.toscrape.com"
  } else {
    url <- paste0("https://books.toscrape.com/catalogue/page-", i, ".html")
  }

page <- read_html(url)
books <- html_elements(page, "article.product_pod")

all_books[[i]] <- tibble(
  title = html_attr(html_element(books, "h3 > a"), "title"),
  rating = html_attr(html_element(books, "p.star-rating"), "class"),
  price = html_text2(html_element(books, "p.price_color")),
  stock = html_text2(html_element(books, "p.availability")),
  image = html_attr(html_element(books, "img.thumbnail"), "src")
)
Sys.sleep(0.5)
}

full_df <- bind_rows(all_books)
full_df$price <- as.numeric(gsub("£", "", full_df$price))

full_df$rating <- gsub("star-rating ", "", full_df$rating)

rating_map <- c(One = 1, Two = 2, Three = 3, Four = 4, Five = 5)

full_df1 <- full_df |> 
  mutate(
    rating_num = rating_map[rating],
    stock = trimws(stock)
  )
table1 <- full_df1 |> 
  count(rating)
library(ggplot2)

Problem 1

ggplot(full_df1, aes(x = rating_num, y = price)) +
  geom_point(color = "blue", alpha = 0.7) +
  geom_smooth(
    method = "lm",
    color = "red",
    se = FALSE,
    linetype = "dotdash"
  ) +
  labs(
    title = "Relationship Between Book Rating and Price",
    x = "Book Rating",
    y = "Book Price (£)",
    caption = "Source: books.toscrape.com"
  ) +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'

Problem 2

ggplot(table1, aes(x = rating, y = n, fill = rating)) +
  geom_col() +
  labs(
    title = "Number of books by rating",
    x = "Star Rating",
    y = "Number of Books",
    caption = "Source: books.toscrape.com"
  ) +
  theme_minimal()

Problem 3

ggplot(full_df1,
       aes(x = rating,
           y = price,
           fill = rating)) +
  geom_boxplot() +
  labs(
    title = "Book Prices by Rating",
    x = "Book Rating",
    y = "Price (£)",
    caption = "Source: books.toscrape.com"
  ) +
  theme_light()