Motivation R and graphics

Paula Andrea Martinez

2018-02-09

The grammar of graphics is a language for describing graphs. Here are some common examples from the package ggplot2 to inspire you on how to use R to show your data.

install.packages("ggplot2")
library(ggplot2)

Scatterplot

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy))

Colour full scatterplots

p <- ggplot(data = mpg, 
            mapping = aes(x = displ, y = hwy, 
                     colour = manufacturer)) + 
       geom_point()
p
p + geom_smooth(method = "lm")

Two plots in one figure environment

Two plots in one figure environmentTwo plots in one figure environment

Stacked barplot

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, 
                         fill = clarity))

Boxplots

library(datasets)

data(airquality)
airquality$Month <- factor(
             airquality$Month,
             labels = c("May", 
                        "Jun", 
                        "Jul", 
                        "Aug", 
                        "Sep"))
ggplot(data = airquality) + 
  geom_boxplot(mapping = 
                 aes(x = Month, y = Ozone))

Polar plot

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = cut),
           width = 1) + 
  coord_polar()

Open source

This handout was written in Rmarkdown, and uses the open-source Tufte style. It has been published on Github pages and also as a PDF handout.

All of the information of my courses can be found on my Github repo R for Data Analysis https://github.com/orchid00/R4da. These resources are freely available under the Creative Commons - Attribution Licence. You may re-use and adapt the material in any way you wish, without asking permission, provided you cite the original source. That is a link back to the website R for Data Analysis and my ORCID 0000-0002-8990-1985.

I acknowledge this publication is resulting from support of Elixir-Belgium for my role as data science and bioinformatics trainer.

Last update: 2018-02-09