book_sales <- read_csv("Books_Data_Clean.csv", show_col_types = FALSE)
head(book_sales)
## # A tibble: 6 Ă— 15
## index `Publishing Year` `Book Name` Author language_code Author_Rating
## <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 0 1975 Beowulf Unkno… en-US Novice
## 2 1 1987 Batman: Year One Frank… eng Intermediate
## 3 2 2015 Go Set a Watchman Harpe… eng Novice
## 4 3 2008 When You Are Engul… David… en-US Intermediate
## 5 4 2011 Daughter of Smoke … Laini… eng Intermediate
## 6 5 2015 Red Queen Victo… eng Intermediate
## # ℹ 9 more variables: Book_average_rating <dbl>, Book_ratings_count <dbl>,
## # genre <chr>, `gross sales` <dbl>, `publisher revenue` <dbl>,
## # `sale price` <dbl>, `sales rank` <dbl>, Publisher <chr>, `units sold` <dbl>
My dataset has a “Publishing Year” column that indicates the year in which a certain book was published. The column only contains a year in YYYY format. In order to turn this more of a date format, I will turn the column into YYYY-MM-DD format by adding 01-01 to the end of each year.
book_sales$`Publishing Year` <- as.Date(paste0(book_sales$`Publishing Year`, "-01-01"), format = "%Y-%m-%d")
head(book_sales)
## # A tibble: 6 Ă— 15
## index `Publishing Year` `Book Name` Author language_code Author_Rating
## <dbl> <date> <chr> <chr> <chr> <chr>
## 1 0 1975-01-01 Beowulf Unkno… en-US Novice
## 2 1 1987-01-01 Batman: Year One Frank… eng Intermediate
## 3 2 2015-01-01 Go Set a Watchman Harpe… eng Novice
## 4 3 2008-01-01 When You Are Engul… David… en-US Intermediate
## 5 4 2011-01-01 Daughter of Smoke … Laini… eng Intermediate
## 6 5 2015-01-01 Red Queen Victo… eng Intermediate
## # ℹ 9 more variables: Book_average_rating <dbl>, Book_ratings_count <dbl>,
## # genre <chr>, `gross sales` <dbl>, `publisher revenue` <dbl>,
## # `sale price` <dbl>, `sales rank` <dbl>, Publisher <chr>, `units sold` <dbl>
Each row in my dataset corresponds to a singular book, along with the aggregation of the units sold and gross sales earned. Due to this, analyzing how a certain book performs over the years is impossible. Throughout these data dives, I have yet to explore the genre feature, so I thought it would be interesting to see how sales by genre change over time.
#removing na values
book_sales <- book_sales[complete.cases(book_sales$`Publishing Year`), ]
#group sales by genre for each year
sales_by_genre <- book_sales |>
group_by(`Publishing Year`, genre) |>
summarise(total_sales = sum(`units sold`))
## `summarise()` has grouped output by 'Publishing Year'. You can override using
## the `.groups` argument.
#create tsibble object
ts_data <- as_tsibble(sales_by_genre, key = genre, index = `Publishing Year`)
#sales by genre over time
ggplot(ts_data, aes(x = `Publishing Year`, y = total_sales, color = genre)) +
geom_line() +
labs(title = "Units Sold by Genre Over Time", x = "Year", y = "Total Units Sold", color = "Genre") +
theme(legend.position = "right")
The visualization shows very little sales until after 1800. This could be due to a mix of less books being sold or just less data being reported, the documentation does not say. Fiction sales seem to dominate, espcially in more recent years.
#linear regression
lm_model <- lm(total_sales ~ as.numeric(year(`Publishing Year`)), data = sales_by_genre)
summary(lm_model)
##
## Call:
## lm(formula = total_sales ~ as.numeric(year(`Publishing Year`)),
## data = sales_by_genre)
##
## Residuals:
## Min 1Q Median 3Q Max
## -65487 -47007 -28590 7626 541874
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -386921.6 105769.1 -3.658 0.00032 ***
## as.numeric(year(`Publishing Year`)) 225.1 54.7 4.116 5.51e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 87960 on 213 degrees of freedom
## Multiple R-squared: 0.07368, Adjusted R-squared: 0.06933
## F-statistic: 16.94 on 1 and 213 DF, p-value: 5.513e-05
The linear model indicates that there is enough data to conclude that there is a significant positive relationship between the publishing year and the number of units sold by genre. The coefficient of the publishing year is indicating that for each year, the number of units sold is estimated ot increase by about 225 units sold.
#smooth the data using moving average
smoothed_data <- sales_by_genre |>
group_by(genre) |>
mutate(smoothed_sales = rollmean(total_sales, k = 5, fill = "extend", align = "right"))
#plot smoothed sales by genre
ggplot(smoothed_data, aes(x = `Publishing Year`, y = smoothed_sales, color = genre)) +
geom_line() +
labs(title = "Smoothed Units Sold by Genre", x = "Year", y = "Smoothed Units Sold", color = "Genre") +
theme(legend.position = "right")
Overall, all genres see an increase in sales over time. Fiction seems to consistently outperform nonfiction books and children’s books, however this could be due to more fiction books in the dataset in general. The long span of time and overall increasing nature of the visualizaiton makes it difficult to see if there are any particular seasonal trends.