Data Visualisation in R Progrming

Data visualisation in R can be done with several packages. However ggplot2 is the most common.

Benefits of ggplot2

1.create diffrent kind of plot 2.Customize the looks and feel of the plot 3.create high quality visual

core concept of ggplot2

  • Aesthetics: the visual property of an object in your plots (e.g size,shape,colour of the data points)
  • Geoms: Geometric object used to represent your data e.g lines, bars, points.
  • Mapping: Matching up a specific variable in your datasets with a specific aesthetic
  • Labels: Adding titles, sub titles, comments on your chat
  • Facets: Lets you display smaller groups or subset of your data (seperate plots for all variables).

load the libraries

library(ggplot2)
library(palmerpenguins)

To view the datasets

{r print(penguins) ## Step 1: use ggplot function to tell R the data to use, + sign is used to add layers

ggplot(data = penguins)+
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

Step 1.start with ggplot fxn and choose the dataset to work with

step 2.Add geom fxn to display your data

step 3.Map the variables you want plot in the argument of the aes()

AESTHETICS

## To split species by colour 
ggplot(data = penguins)+
  geom_point(mapping = aes(x=flipper_length_mm,y=body_mass_g,colour = species))

To split species by shape

ggplot(data=penguins)+
  geom_point(mapping = aes(x=flipper_length_mm,y=body_mass_g,shape = species,colour = species))

To split the species by size

ggplot(data = penguins)+
  geom_point(mapping = aes(x=flipper_length_mm,y=body_mass_g,size=species,shape=species))

To give the entire point a single plot

ggplot(data = penguins)+
  geom_point(mapping = aes(x=flipper_length_mm,y=body_mass_g),color="purple")

Geom_function

Geom-smooth: To show the trend of the relationship

ggplot(data = penguins)+
  geom_smooth(mapping = aes(x=flipper_length_mm,y=body_mass_g))+
  geom_point(mapping = aes(x=flipper_length_mm,y=body_mass_g))

To show the species in different lines

ggplot(data = penguins)+
  geom_smooth(mapping = aes(x=flipper_length_mm,y=body_mass_g,linetype = species))

Geom_bar

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

To add legend to the chart

ggplot(data = penguins)+
  geom_bar(mapping = aes(x=species,fill = sex))

PIE CHART

To create the new table for the chart

library(dplyr)
car_count <- mtcars%>%
  count(cyl)%>%
  rename(count = n)

To create a pie chart

ggplot(car_count,aes(x="",y=count,fill=factor(cyl)))+
  geom_bar(stat="identity",width = 1)+
  coord_polar(theta = "y")+
  theme_void()

Facets: displays data in subsets or group

e.g in the penguins we want to seperate plots for each of the species

ggplot(data = penguins)+
  geom_smooth(mapping = aes(x=flipper_length_mm,y=body_mass_g,colour = species))+
  geom_point(mapping = aes(x=flipper_length_mm,y=body_mass_g,colour = species))+
  facet_wrap(~species)

Annotate = To add notes to a diagram or documennts to explain or comment on it

Labels fxn (labs) can add the following: title, subtitle, captions, axis title

Adding a title to the penguin correlation plot

ggplot(data = penguins)+
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, colour = species))+
  labs(title = "palmer penguins: Body mass vs Flipper length", subtitle = "sample of three penguins specie",
  caption = "chart plotted by DAVID uSOJI")+
  annotate("text", x = 220, y = 3500, label = "chinstrap is the largest cohort", color="blue",
           fontface="bold",size=4.5, angle=25)+
  annotate("text", x = 180, y  = 5500, label = "Gentoo is the largest cohort", colour="maroon",
           fontface="bold", size=4.5, angle=25)