Introduction to data analysis using R, R Studio and R Markdown
Dee Chiluiza, PhD
Northeastern University
Boston, Massachusetts

 
Short manual series: Strategies to present graphs and images


Purpose:


To present and combine multiple graphs and tables for better presentation.


List of topics

Insert images
- Control size and insert images using knitr::include_graphics()
- How to reduce white space around figures
- Using grid.extra::grid.arrange()
- Using patchwork library
- Use patchwork to control width of each figure
- Add annotations using Geom Text and ggrepel
- Combination of fig.chunk and HTML codes.
- Highlight data in graphs using geom_mark
- Using Colors: RGB, RColorBrewer, Terrain colors, Custom colors.


Control size and insert images using knitr::include_graphics()

# {r, fig.align = 'center', out.width="40%", out.height="50%", fig.width = 8, fig.height = 6}

knitr::include_graphics("Images/bruno.JPG") 



How to reduce white space around figures

# Data sets (add importing codes here)
library(readxl)
library(readr)
M2Data <- read_excel("DataSets/M2Project_V2.xlsx")

t4Africa = dplyr::filter(M2Data, Market=="Africa")

t4table = table(t4Africa$Product_Category)

Figure chunk. Compare size of left and right figures.

# {r, dev='png', fig.align='center', out.width="100%"}

par(mfrow=c(1,2))

t4pie1 = pie(table(t4Africa$Product_Category))

par(mar = c(0.1, 0.1, 0.1, 0.1))        # Reduce space around plots

t4pie2 = pie(table(t4Africa$Product_Category))



Using grid.extra::grid.arrange()

Check the grid.arrange() code after graphs were created. Also notice that graphs require names.

aa1 = ggplot(data=faithful,
       mapping = aes(x=eruptions, y=waiting))+
  geom_point()

# Different grammar 

aa2= ggplot(faithful)+
  geom_point(aes(x=eruptions, y=waiting))

# Mapping color

aa3= ggplot(faithful)+
  geom_point(aes(x=eruptions, 
                 y=waiting,
                 color=eruptions<3))

# Set color, not mapping, outside aes()

aa4=ggplot(faithful)+
  geom_point(aes(x=eruptions, 
                 y=waiting),
                 color="red")

grid.arrange(aa1, aa2, aa3, aa4, ncol=2, nrow=2)

aa3= ggplot(faithful)+
  geom_point(aes(x=eruptions, 
                 y=waiting,
                 color=eruptions<3))

aa4=ggplot(faithful)+
  geom_point(aes(x=eruptions, 
                 y=waiting),
                 color="red")

grid.arrange(aa3, aa4, nrow=1)


One over two layout

mpg_counted = mpg %>% 
  count(class, name = 'count')

barr1 = ggplot(mpg_counted) + 
  geom_bar(aes(x = class, y = count), stat = 'identity')

# It's the same as the code below
barr2 = ggplot(mpg) + 
  geom_bar(aes(x = class))

# Flip and remove background
barr3 = ggplot(mpg) + 
  geom_bar(aes(x = class)) +
  coord_flip() + 
  theme_minimal()+
  labs(x = "Counting", y = "Car Classes")

grid.arrange(arrangeGrob(barr1, barr2, ncol=2),
             barr3, ncol=1)



Using patchwork library

library(patchwork)
p1 <- ggplot(msleep) + 
  geom_boxplot(aes(x = sleep_total, y = vore, fill = vore))

p2 <- ggplot(msleep) + 
  geom_bar(aes(y = vore, fill = vore))

p3 <- ggplot(msleep) + 
  geom_point(aes(x = bodywt, y = sleep_total, colour = vore)) + 
  scale_x_log10()

p1 + p2 + p3

The code below can be p1/ (p2|p3) or p1/ (p2+p3).

p1/ (p2|p3)

Another way.

(p1 | p2) / p3

p_all <- (p1 | p2) / p3
p_all + plot_layout(guides = 'collect')

p_all & theme(legend.position = 'none')

p_all <- p_all & theme(legend.position = 'none')
p_all + plot_annotation(
  title = 'Mammalian sleep patterns',
  tag_levels = 'A'
)

Use patchwork to control width of each figure

Change the width of each individual figure.

p <- ggplot(mtcars) + 
  geom_point(aes(x = disp, y = mpg))
p + p + p

pthree = p + p + p

pthree + plot_layout(widths = c(3,2,1))



Add annotations using Geom Text and ggrepel

Standard geom_text will often result in overlapping labels.

ggplot(mtcars, aes(x = disp, y = mpg)) + 
  geom_point() + 
  geom_text(aes(label = row.names(mtcars)))

ggrepel takes care of that, link: ggreprl.
- geom_text_repel()
- geom_label_repel()

Link to ggrepel examples: https://ggrepel.slowkow.com/articles/examples.html.

library(ggrepel) # From package ("ggrepel")
ggplot(mtcars, aes(x = disp, y = mpg)) + 
  geom_point(color = 'red') +
  geom_text_repel(aes(label = row.names(mtcars)), size=3)+
  theme_classic(base_size = 16)



Combination of fig.chunk and HTML codes.

# Data sets (add importing codes here)
library(readxl)
library(readr)
M2Data <- read_excel("DataSets/M2Project_V2.xlsx")

t4Africa = dplyr::filter(M2Data, Market=="Africa")

t4table = table(t4Africa$Product_Category)


Figure chunk.

par(mar = c(0.1, 0.1, 0.1, 0.1))        # Reduce space around plots

t4pie = pie(table(t4Africa$Product_Category))
Var1 Freq
Furniture 13
Office Supplies 11
Technology 30
A nice plot.



Highlight data in graphs using geom_mark

To highlight certain parts of the data, and to add annotations, use the geom_mark_*() family of geoms.

library(ggforce)
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(color="#A11515") + 
  geom_mark_ellipse(aes(filter = gear == 4,
                        label = '4 gear cars',
                        description = 'Cars with fewer gears 
                        tend to both have higher yield and 
                        lower displacement'))+
theme_classic(base_size = 16)


Using Colors

RGB Color Codes Chart: https://www.rapidtables.com/web/color/RGB_Color.html

# {r, fig.align = 'center', out.width="50%", out.height="20%"}

knitr::include_graphics("Images/rgb.JPG") 


How to use colors on graphs.

  1. I have three favorite color palettes. one is RColorBrewer.
    To see all colors use display.brewer.all(), this will display an image with all color sets and their corresponding names.
    Using this palete, you can select the color set ant the number of color you need.
    For example: If we have a graph with 6 bars, we can have a preview of the color combination by calling the color set and number of colors needed. display.brewer.pal(6,“Dark2”)


# {r col1, fig.align = 'center', out.width="60%", fig.asp = 1}

par(mai=c(0.2,0.6,0.2,0.2), mfcol=c(1,3), mar=c(0.2,3,0.2,0.2))

col1a = display.brewer.all()

col1b = display.brewer.pal(8,"Dark2")

col1c = display.brewer.pal(9,"Greens")

Use the RColorBrewer() in bar plots.

# {r col2, fig.align="center"}

par(mfrow=c(1,2), mar=c(1,2,1.2,1.2), mai=c(0.2,0.5,0.2,0.2))

barcol = c(10, 8, 6, 5, 7, 9)

col2a = barplot(barcol,
           col = brewer.pal(6,"Dark2"),
           las=1,
           ylim=c(0,12))

col2b = barplot(barcol,
           col = brewer.pal(6,"Greens"), 
           las=1,
           ylim=c(0,12))

| Custom


Make your own palettes.

par(mfrow=c(1,2), mar=c(1,2,1.2,1.2), mai=c(0.2,0.4,0.6 ,0.2))

# My colors option 1
mycolors1 = c("blue", "yellow", "green", "pink", "lightblue", "cyan")

# My colors option 2
mycolors2 = c("#E96464", "#E564E9", "#A764E9", "#6483E9", "#64E9A7", "#F7F36D")

# Application
col2a = barplot(barcol,
                col = mycolors1, 
                main= "Using mycolors 1", 
                las=1,
                ylim=c(0,12))
col2b = barplot(barcol, 
                col = mycolors2, 
                las=1,
                main= "Using my colors 2", 
                ylim=c(0,12))


Disclaimer


Disclaimer: This short series manual project is a work in progress. Until otherwise clearly stated, this material is considered to be draft version.


Dee Chiluiza, PhD
June 2021
Last update: 12 October, 2021
Boston, Massachusetts, USA

Bruno Dog