Author: Stefan

Graphs in R / Aesthetics in ggplot2

ggplot2 – the aes() element in ggplot The aim is to have a clear plot of your data. Here some examples :

  Plots with a clear output Plot 1,2,3, 6 and 11   Plots with confusing elements Plot

Graphs in R / Data formatting

Working with tidyr package A dataset is ‘tidy’ when every row is 1 observation A dataset is ‘wide’ when a row contains more observations In R tidyr package ….  

Result

In Plots

 With the wide dataset

Graphs in R / Basic Plot vs ggplot2

Basic Plot with lm()

  ggplot2 with geom_smooth() method lm

In ggplot the lm() is being composed within the geom_smooth() element, and choosing “lm” (linear model).  

Graphs in R / Basic Plot function – linear model

Basic Plot – linear model with abline() Plot 1 – linear model all datapoints

  Plot 2 – linear model per subset Add library(graphics) to the code for the legend() function

Use of lapply –> take the vector

Graphs in R / ggplot2 – Diamonds

Part 2 – Scatterplot and additional plots Scatterplot with smoothed line

Scatterplot with 3rd variable and based on smoothed line x-axis = carat / y-axis = price / 3rd variable = clarity

 

To omit the grey zone

Graphs in R / ggplot2

First Graphs – Scatterplot 2 variables

The x-axis is now handled as factor. Graphs – Scatterplot with 3 variables This can be done by either add a color or a size. But this is not optimal ( colors can