ggplot2

Easy ggplot2 Theme customization with {ggeasy}

In this post, We’ll learn about {ggeasy} an R package by Jonathan Carroll. The goal of {ggeasy} is to help R programmers make ggplot2 theme customizations with simple-easy English functions. (much easier than playing with theme()) We use dataset generated by {fakir} for this tutorial. Youtube: https://youtu.be/iAH1GJoBZmI Video Tutorial Code library(fakir) library(tidyverse) library(ggeasy) # generate dataset clients <- fakir::fake_ticket_client(100) # rotate x axis labels clients %>% count(state) %>% ggplot() + geom_col(aes(state,n)) + easy_rotate_x_labels() # color the text and increase text size clients %>% count(state) %>% ggplot() + geom_col(aes(n,state), fill = "orange") + easy_text_color("orange") + easy_text_size(25, teach = TRUE) # move legend position clients %>% count(state, source_call) %>%# View() ggplot() + geom_col(aes(n,state, fill = source_call)) + #easy_move_legend("bottom", teach = TRUE) theme(legend.

How to reorder arrange bars with in each Facet of ggplot

One of the problems that we usually face with ggplot is that rearranging the bars in ascending or descending order. If that problem is solved using reorder() or fct_reorder(), the next problem is when we have facets and ordering bars within each facet. Recently I came acrosss this function reorder_within() from the package tidytext (Thanks to Julia Silge and Tyler Rinker - who created this solution originally) Example Code: library(tidyr) library(ggplot2) iris_gathered <- gather(iris, metric, value, -Species) ggplot(iris_gathered, aes(reorder(Species, value), value)) + geom_bar(stat = 'identity') + facet_wrap(~ metric) As you can see above, the bars in the last facet isn’t ordered properly.

How to arrange ggplot (barplot) bars in ascending or descending order

One of the reasons you’d see a bar plot made with ggplot2 with no ascending / descending order - ordering / arranged is because, By default, ggplot arranges bars in a bar plot alphabetically. But most of the times, it would make more sense to arrange it based on the y-axis it represents (rather than alphabetically). It could be your month-wise time series or high-medium-low bars - these are some examples where an alphabetically-sorted bar chart wouldn’t make sense in fact would hinder the process of data communication.

How to make your Facet Wrap / Facet Grid ggplots span across multiple pages in PDF

This is going to be a very short post. It’s a problem I faced a few days back and after some DDGing, Github Issues scanning and some SOing - I found the answer. So, I thought to share it - for archival and also my future reference. Problem The problem here is when you use facet_grid() or facet_wrap() of ggplot inside an R chunk of an Rmd (Rmarkdown) file. The rendered PDF wouldn’t show those faceted multiple plots in multiple pages instead just embedded one on top another in the same page.