To code or not to code, that is the question...
Describes Information/ideas/concepts from any source domain.
GEOMETRY as the target domain : What comes out of R is predominantly "geometry"
all ggplot2
aes(x = , y = )
(aesthetics)aes(x = , y = , color = )
(add color)aes(x = , y = , size = )
(add size)+ facet_wrap(~ )
(facetting)+ scale_
( add a scale)+ geom_*()
geom_point()
geom_line()
geom_histogram()
geom_boxplot()
geom_bar()
or geom_col
(see Lab 02)head(penguins)
## # A tibble: 6 × 8## species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int>## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007## 3 Adelie Torgersen 40.3 18 195 3250 female 2007## 4 Adelie Torgersen NA NA NA NA <NA> 2007## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007
We see the first few rows of the dataset penguins
. We see that there are a few NA data observations too. Let us remove them for now.
penguins <- penguins %>% drop_na()
ggplot(penguins)
ggplot(data = penguins, mapping = aes(x = bill_length_mm, y = body_mass_g))
ggplot(data = penguins, mapping = aes(x = bill_length_mm, y = body_mass_g)) + geom_point()
ggplot(data = penguins, mapping = aes(x = bill_length_mm, y = body_mass_g)) + geom_point() + geom_smooth(method = "lm")
ggplot(data = penguins)
ggplot(data = penguins, aes(x = bill_length_mm, y = body_mass_g, color = island))
We can leave out the "mapping" word and just use aes .
Why is there no plot?
🤔 💭
Right !! We have not used a geom
command yet!!
ggplot(data = penguins, aes(x = bill_length_mm, y = body_mass_g, color = island)) + geom_point() + ggtitle("A point geom with position, color aesthetics")
Note that the points are located by position coordinates on both x and y axis, and coloured by the island variable.
ggplot(data = penguins, aes(x = bill_length_mm, y = body_mass_g, color = island)) + geom_point(size = 4) + ggtitle("A point geom with position color and size aesthetics")
Note that the points are located by position coordinates on both x and y axis, and coloured by the island variable.
And we've fixed size = 4!
diamonds %>% # Sample some 20% of the data slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price))
Are the points all overlapping? Can we see them better?
diamonds %>% # Sample some 20% of the data slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price), # alpha outside the aes() !!! alpha = 0.2) + labs(title = "Points plotted with Alpha")
Are the points all overlapping? Can we see them better?
ggplot(diamonds) + geom_boxplot(aes(x = cut, y = price)) + labs(title = "Box Plot")
ggplot(diamonds) + geom_boxplot(aes(x = cut, y = price, fill = cut)) + labs(title = "Box Plot")
ggplot(data = penguins)
ggplot(data = penguins) + aes(x = species)
ggplot(data = penguins) + aes(x = species) + geom_bar() + ggtitle("A bar geom with position and height aesthetics")
The bars are plotted with positions on the x-axis, defined by the species
variable, and heights mapped to the y-axis.
How did the graph "know" the heights of the bars?
geom_bar
has an internal count
statistic computation.
Many geom_s
have internal computation that are accessible to programmers.
When using more than a pair of variables with a bar chart, we have a few more position options:
ggplot(penguins, aes(x = species, fill = island))
When using more than a pair of variables with a bar chart, we have a few more position options:
ggplot(penguins, aes(x = species, fill = island)) + geom_bar() + ggtitle(label = "A stacked bar chart")
The bars are coloured by the island
variable and are stacked in position.
And here we use the dodge
option:
ggplot(penguins, aes(x = species, fill = island)) + geom_bar(position ="dodge") + ggtitle(label = "A dodged bar chart")
ggplot(penguins)
ggplot(penguins) + aes(x = flipper_length_mm, y = body_mass_g)
ggplot(penguins) + aes(x = flipper_length_mm, y = body_mass_g) + geom_point()
ggplot(penguins) + aes(x = flipper_length_mm, y = body_mass_g) + geom_point() + facet_wrap(~island) + ggtitle("A point geom graph with facets")
The graph has split into multiples, based on the number of islands.
ggplot(penguins) + aes(x = flipper_length_mm, y = body_mass_g) + geom_point()
What if we have even more "factor" variables?
We have island
and species
...can we split further?
ggplot(penguins) + aes(x = flipper_length_mm, y = body_mass_g) + geom_point() + facet_grid(species~island) + ggtitle("A point geom graph with grid facets")
The graph has split into multiples, based on the number of islands and the number of species.
diamonds %>% slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price))
diamonds %>% slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price, colour = cut), size = 3) + scale_colour_brewer(palette = "Set3") + labs(title = "Brewer Colour Pallette (Set3)")
We are using the RColorBrewer
package here.
Type RColorBrewer::display.brewer.all()
in your Console and see what palettes are available.
diamonds %>% slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price, colour = cut), size = 3) + scale_colour_viridis_d() + labs(title = "Viridis Palette", subtitle = "The Default in ggplot")
diamonds %>% slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price, colour = cut), size = 3) + scale_colour_viridis_d(option = "magma") + labs(title = "Viridis Palette, Option Magma")
diamonds %>% slice_sample(prop = 0.2) %>% ggplot(.) + geom_point(aes(x = carat, y = price, colour = cut), size = 3) + scale_colour_viridis_d(option = "inferno") + labs(title = "Viridis Palette, Option Inferno")
ggplot
takes a dataframe/tibble as the data argumentaes
-thetic arguments can be x
, y
, colour
, shape
, alpha
for example...geom_*()
commands specify the kind of plotggplot
package offers a Grammar of near-English commands which allow us to plot data in various ways. To code or not to code, that is the question...
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |