Day 3 - Time to draw some lines ✍️ (introducing graphs)

Hello 

I'm a little late today but don't worry. We're still learning a new concept. That's a commitment I made and we WILL show up to learn R every. single. day.

Why am I so pumped?

I just realised that there are 140 people learning R.

Together.

From this email.

140... people...

Do you realise how big that is?


I'm am psychheeeeedddd


And it would NOT be possible without you!

Anyhoo...

A quick recap

Yesterday, we learned how to view data sets.

If you're new, don't worry I'll be uploading that to the archive at rainarangelo.com soon! (just bear with me because it takes time 😅). If you're in a super hurry, reply to this email and I'll send you yesterday's email.

Let's do the customary library loads before we begin

library(tidyverse)
library(palmerpenguins)
library(ggthemes)

Copy the code above, paste it in your console and hit enter.

If you haven't installed them (because it's your first day refer to installing and loading packages here)

Time to create a graph

We have a goal with the penguins dataset.

Display the relationship between flipper lengths and body masses of these penguins

To do that, we need to plot them on a graph

For that?

We need 

  1. A canvas / graph paper

  2. Map x and y axes

  3. Your visualisation approach

Let's begin with the canvas

Our data set is penguins.

To start with a canvas, all we have to do is

ggplot(data=penguins)

You'll see on the right side, under 'plots' a blank gray box.

Yup... that's it.

Not the most exciting but it's good to know what this does :)

Let's move on to mapping the x and y axes.

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


You can see the x and y axes mapped with the variables selected :)

And now...

For the best part

Visualising the data

For this graph, we'll go with a simple scatterplot.

The 'geom_' is what will help us describe the type of plot.

For a scatterplot, the geom is geom_point()

(Similary, you have geom_bar() and geom_line() for bar and line graphs)

When we put the previous line of code together with geom_point here's what we get

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

*drumroll*



Ignore the error for now (it's basically because the data has two rows of incomplete data)

But yeah! that's what you'll get when you put the three pieces of a graph together

Congratulations ! You made your first graph in R

Tomorrow, we'll make this graph colourful (I must admit it's a little dull right now 👀), but that's enough for today!

Talk tomorrow

and hey...

If you're finding this a lot to digest, reply and let me know

I'm here to help!

Previous
Previous

Day 4: Plotting using colour and shapes

Next
Next

Day 2: Load, View Data From a Data Set