[This article was first published on Deeply Trivial, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
One of the easiest ways to make a beautiful ggplot is by using a theme. ggplot2 comes with a variety of pre-existing themes. I’ll use the genre statistics summary table I created in yesterday’s post, and create the same chart with different themes.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
library(tidyverse) ## -- Attaching packages ------------------------------------------- tidyverse 1.3.0 -- ## <U+2713> ggplot2 3.2.1 <U+2713> purrr 0.3.3 ## <U+2713> tibble 2.1.3 <U+2713> dplyr 0.8.3 ## <U+2713> tidyr 1.0.0 <U+2713> stringr 1.4.0 ## <U+2713> readr 1.3.1 <U+2713> forcats 0.4.0 ## -- Conflicts ---------------------------------------------- tidyverse_conflicts() -- ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag() reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allrated.csv", col_names = TRUE) ## Parsed with column specification: ## cols( ## Title = col_character(), ## Pages = col_double(), ## date_started = col_character(), ## date_read = col_character(), ## Book.ID = col_double(), ## Author = col_character(), ## AdditionalAuthors = col_character(), ## AverageRating = col_double(), ## OriginalPublicationYear = col_double(), ## read_time = col_double(), ## MyRating = col_double(), ## Gender = col_double(), ## Fiction = col_double(), ## Childrens = col_double(), ## Fantasy = col_double(), ## SciFi = col_double(), ## Mystery = col_double(), ## SelfHelp = col_double() ## ) genrestats <- reads2019 %>% filter(Fiction == 1) %>% arrange(OriginalPublicationYear) %>% group_by(Childrens, Fantasy, SciFi, Mystery) %>% summarise(Books = n(), WomenAuthors = sum(Gender), AvgLength = mean(Pages), AvgRating = mean(MyRating)) genrestats <- genrestats %>% bind_cols(Genre = c("General Fiction", "Mystery", "Science Fiction", "Fantasy", "Fantasy SciFi", "Children's Fiction", "Children's Fantasy")) %>% ungroup() %>% select(Genre, everything(), -Childrens, -Fantasy, -SciFi, -Mystery) genre <- genrestats %>% ggplot(aes(Genre, Books)) + geom_col() + scale_y_continuous(breaks = seq(0,20,1))Since I’ve created a new object for my figure, I can add a theme by typing genre + [theme]. Here’s a handful of the ggplot2 themes.
library(ggthemes) ## Warning: package 'ggthemes' was built under R version 3.6.3 genre + theme_economist_white() + theme(plot.background = element_rect(fill = "lightblue"))