Etc1010 graphing

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

Basic template for graphing

Code: ‘

ggplot(data, aes(x = ..., y = ...)) + geom_... + labs(

2
New cards

What are the main geom functions used

  • geom_point() – scatter plot

  • geom_line() – line plot

  • geom_col() – bar plot (height = value)

  • geom_bar() – counts automatically

  • geom_boxplot() – boxplots

  • geom_histogram() – histogram

  • geom_smooth() – adds trend line

<ul><li><p class=""><code>geom_point()</code> – scatter plot</p></li><li><p class=""><code>geom_line()</code> – line plot</p></li><li><p class=""><code>geom_col()</code> – bar plot (height = value)</p></li><li><p class=""><code>geom_bar()</code> – counts automatically</p></li><li><p class=""><code>geom_boxplot()</code> – boxplots</p></li><li><p class=""><code>geom_histogram()</code> – histogram</p></li><li><p class=""><code>geom_smooth()</code> – adds trend line</p></li></ul><p></p>
3
New cards

When do you use fill vs colour in ggplot2?

What does each one affect?
Which geoms use them?

FILL

  • Fills the inside of shapes (like bars, boxes, densities).

  • Used in: geom_bar(), geom_col(), geom_boxplot(), geom_density(), geom_histogram(), geom_area().

Example: ggplot(data, aes(x, fill = group)) + geom_bar()

This makes sure each group has a different colour

COLOUR

  • Changes the border of shapes or the color of lines/points.

  • Used in: geom_line(), geom_point(), geom_bar(), geom_boxplot()

Example: ggplot(data, aes(x, colour = group)) + geom_point()

4
New cards

Code: facet_grid()

Creates a matrix of plots: one variable for rows, another for columns.
Useful for seeing interaction effects between two categorical variables.

Eg.
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(class ~ drv)

  • This will create a grid of scatter plots where each row corresponds to a different car class and each column corresponds to a different drive type drv

<p>Creates a matrix of plots: one variable for rows, another for columns.<br>Useful for seeing interaction effects between <strong>two categorical variables.</strong></p><p></p><p>Eg. <br>ggplot(mpg, aes(displ, hwy)) +  geom_point() +  facet_grid(class ~ drv)</p><p></p><ul><li><p class="">This will create a grid of scatter plots where each row corresponds to a different car class and each column corresponds to a different drive type drv</p></li></ul><p></p>
5
New cards

Code: facet_wrap

Creates individual plots for each level of one categorical variable in a grid layout.
Use when you want to split data into subplots based on one factor.

Eg. ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~ class)

  • This will create a series of scatter plots, each representing a different class of car laid out in a single row or column

<p>Creates individual plots for each level of <strong>one categorical variable</strong> in a grid layout.<br>Use when you want to split data into subplots based on one factor.</p><p></p><p>Eg. ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~ class)</p><p></p><ul><li><p>This will create a series of scatter plots, each representing a different class of car laid out in a single row or column</p></li></ul><p></p>
6
New cards

How to add labels

Code: labs()

Eg. ggplot(data, aes(x, y)) + geom_point() + labs(title = " Title", x = "X Axis Label", y = "Y Axis Label", colour = "Group”)

Colour=”Group” shows the legend title

7
New cards

Code: position = “fill”

Stacks the bars in a way that each bar's height is proportional to the total

Eg. ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill")

<p>Stacks the bars in a way that each bar's height is proportional to the total</p><p></p><p>Eg. ggplot(tb_au, aes(x = year, y = count, fill = gender)) + geom_col(position = "fill")</p><p></p>
8
New cards

geom_point()

Used for scatter plot

Shows correlation between x and y

9
New cards

Geom_line()

Line Plot

Shows trends over time

10
New cards

Geom_col()

Bar Plot where height is based on values in dataset

11
New cards

Geom_bar()

Bar Plot where height (y) of bar is the number of times x has occurred

12
New cards

Geom_boxplot()

Box Plot

Shows IQR, median, max and min of values

<p>Box Plot</p><p>Shows IQR, median, max and min of values </p>
13
New cards

Geom_histogram()

Histogram

Shows distribution of values

14
New cards

Geom_smooth()

Adds smooth line to show trends in scatter plots

Eg.

ggplot(data, aes(x, y)) + geom_point() + geom_smooth()

15
New cards

Code: ylim()

Sets the min and max of the y axis

Eg. geom_point() + ylim(0,100)

16
New cards

Code: position=”dodge”

Plots side-by-side bars instead of stacked

Opposite of posiiton=fill

Eg. geom_col(position=”dodge”)

<p>Plots side-by-side bars instead of stacked</p><p>Opposite of posiiton=fill</p><p></p><p>Eg. geom_col(position=”dodge”)</p>
17
New cards

Code: “position=stack”

Bars are stacked on top of each other

Different from code: “position=fill” because heights are not scaled to 100%

18
New cards

geom_vline()

Adds a vertical line on graph

Eg. geom_vline(xintercept = avg_hp,

linetype = "dotted",

size = 4,

alpha = 0.6,

color = "green")

This adds a vertical line where x=avg_hp

Linetype: makes the line dotted

Size: Changes thickness of line

Alpha: Makes the line slightly transparent (0=invisible, 1=opaque)

Colour: Makes the line green

19
New cards

Geom_text()

Adds a label to the line

Eg. geom_text(aes(x = avg_hp + 3, y = 30),

label = "Mean",

angle = 45,

colour = "blue",

size = 7)

avg_hp+3: This is the x position of the text

y=30: This is the y position of the text

label: The word that’s displayed

Colour: Changes the colour of the word

Size: changes the size of the word

20
New cards

Geom_hline()

Adds a horizontal line to graph

Eg. geom_hline(yintercept = avg_mpg,

linetype = "dashed",

size = 1.5,

alpha = 0.7,

color = "purple")

yintercept: Shows where the line will cross y axis

linetype=makes it a dashed line

size: Changes the thickness

Colour: Changes the colour

21
New cards

geom_text()

Adds text to graphs:

Example:

geom_text(data=mpg, aes(label = model), size=4)

data: shows dataset being used

label: showing the model name as the label

size=size of the font

22
New cards

geom_label

Same as geom_text but has a rectangle behind writing

Eg. geom_label(aes(label = model),

data = best,

nudge_y = 2)

label=showing the model name as the label

data: shows dataset being used

23
New cards

Code: nudge_y

Pushes the label up or down

Eg. (nudge_y=2)

Label moves 2 points up

24
New cards

Code: nudge_x

Moves label side to side

Eg. (nudge_x=2)

25
New cards

Code: “geom_label_repel()”

Places labels on graph and automatically prevents any overlap

Eg. geom_label_repel(data = best, aes(label = model, colour=model))

26
New cards

Code: “fct_reorder()”

Orders variables based on median of n

Eg.

ggplot(aes( x = fct_reorder(manufacturer, n), y = n))

27
New cards

Code: “Scale_x_continuous()”

Customises numbers on x axis

Eg. ggplot(data, aes(x = var1, y = var2)) +

geom_point() +

scale_x_continuous(breaks = seq(0, 100, by = 10))

x-axis goes from 0,100 and counts by 10

28
New cards

Code: “Scale_y_continuous()”

Customises numbers on y axis

Eg. ggplot(data, aes(x = var1, y = var2)) +
geompoint() + scale_y_continuous(breaks = seq(0, 100, by = 10))

y-axis goes from 0,100 and counts by 10

29
New cards

Code: “scale_fill_discrete()”

Changes names of legend

Eg. scale_fill_discrete(breaks = c("f", "m"), labels = c("female", "male"))

In the legend, changes f to female and m to male

30
New cards