Ggplot2 Cheat Sheet R



R data cleaning cheat sheetGgplot2 cheat sheet rules

Even the most experienced R users need help creating elegant graphics. The ggplot2 library is a phenomenal tool for creating graphics in R but even after many years of near-daily use we still need to refer to our Cheat Sheet. Up until now, we’ve kept these key tidbits on a local PDF. Get the Ultimate R Cheat Sheet here: Learn R here: https://university.business-science.io/p/ds4b-101-r-busi.

In this section we make histograms and density plots for the albacore tuna catch.

A non-empty plot will be made once you state what sort of plot you wish to make (e.g. histogram or density plot), which is referred to as the geometry for the plot. In this case we wish to plot x (i.e. ALB.mt) as a histogram, so we use the function geom_histogram. Refer to the ggplot2 cheat sheet for the other type of plots you can make for a single column of data (called One Variable in the cheat sheet)

As choosing the bin width is often best done manually, to explore what patterns there are in the data, the function suggests using the binwidth argument to do this. The default is to use 30 bins for the plot.

You can give arguments to the geom_histogram function for the bin width and fill colour of the histogram. The default bin width looks pretty reasonable, but I’ve a chosen something different to illustrate setting the bin width.

Now do a histogram with bins of size 10, and fill the rectangles with another colour. Memory manager for mac os x.

Use theme_set() to change the default plot theme to have a white background.

In the next plot we explicitly define the bin widths using the breaks argument. The first bin goes from 0 to 20 and is centred on 10 metric tonnes.

To change the interval width in function seq(), modify the value of the by= argument to 50.

Ggplot2 Cheat Sheet Rules

If we wish to make a density plot we change the “geometry” by using the geom_density function. The argument adjust for this function determines how smooth the density plot is, with higher values giving smoother plots. Capture video on screen for mac.