While the song is playing...
Draw a mental model / concept map of last lectures content on joins.
There are three main types of colour palette:
## # A tibble: 157,820 x 5## country year count gender age ## <chr> <dbl> <dbl> <chr> <chr>## 1 Afghanistan 1980 NA m 04 ## 2 Afghanistan 1981 NA m 04 ## 3 Afghanistan 1982 NA m 04 ## 4 Afghanistan 1983 NA m 04 ## 5 Afghanistan 1984 NA m 04 ## 6 Afghanistan 1985 NA m 04 ## 7 Afghanistan 1986 NA m 04 ## 8 Afghanistan 1987 NA m 04 ## 9 Afghanistan 1988 NA m 04 ## 10 Afghanistan 1989 NA m 04 ## # … with 157,810 more rows
## # A tibble: 219 x 4## country `2002` `2012` reldif## <chr> <dbl> <dbl> <dbl>## 1 Afghanistan 6509 13907 1.14 ## 2 Albania 225 185 -0.178 ## 3 Algeria 8246 7510 -0.0893## 4 American Samoa 1 0 -1 ## 5 Andorra 2 2 0 ## 6 Angola 17988 22106 0.229 ## 7 Anguilla 0 0 0 ## 8 Antigua and Barbuda 4 1 -0.75 ## 9 Argentina 5383 4787 -0.111 ## 10 Armenia 511 316 -0.382 ## # … with 209 more rows
ggplot(tb_map) + geom_polygon(aes(x = long, y = lat, group = group, fill = reldif)) + theme_map()
library(viridis)ggplot(tb_map) + geom_polygon(aes(x = long, y = lat, group = group, fill = reldif)) + theme_map() + scale_fill_viridis(na.value = "white")
ggplot(tb_map) + geom_polygon(aes(x = long, y = lat, group = group, fill = reldif)) + theme_map() + scale_fill_distiller(palette = "PRGn", na.value = "white", limits = c(-7, 7))
viridis
, and scico
.p2 <- p + scale_colour_brewer(palette = "Dark2")p2
p3 <- p + scale_colour_viridis_d()p3
+ scale_colour_viridis()
+ scale_colour_brewer(pallete = "Dark2")
scico
R packageBasic rule: place the groups that you want to compare close to each other
Which plot answers which question?
Here are two different arrangements of the tb data. To answer the question "Is the incidence similar for males and females in 2012 across age groups?" the first arrangement is better. It puts males and females right beside each other, so the relative heights of the bars can be seen quickly. The answer to the question would be "No, the numbers were similar in youth, but males are more affected with increasing age."
The second arrangement puts the focus on age groups, and is better to answer the question "Is the incidence similar for age groups in 2012, across gender?" To which the answer would be "No, among females, the incidence is higher at early ages. For males, the incidence is much more uniform across age groups."
ggplot(df, aes(x = x, y = y1)) + geom_point()
ggplot(df, aes(x = x, y = y1)) + geom_point() + geom_smooth(method = "lm", se = FALSE)
ggplot(df, aes(x = x, y = y1)) + geom_point() + geom_smooth(method = "lm")
ggplot(df, aes(x = x, y = y2)) + geom_point()
ggplot(df, aes(x = x, y = y2)) + geom_point() + geom_smooth(method = "lm", se = FALSE)
ggplot(df, aes(x = x, y = y2)) + geom_point() + geom_smooth(se = FALSE)
ggplot(df, aes(x = x, y = y2)) + geom_point() + geom_smooth(se = FALSE, span = 0.05)
p1 <- ggplot(df, aes(x = x, y = y2)) + geom_point() + geom_smooth(se = FALSE, span = 0.2)p1
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg, colour = factor(gear))) + facet_wrap(~am)p
p + theme_minimal()
theme_few()
p + theme_few() + scale_colour_few()
theme_excel()
🤒p + theme_excel() + scale_colour_excel()
library(wesanderson)p + scale_colour_manual( values = wes_palette("Royal1") )
ggthemes
package has many different styles for the plots. xkcd
, skittles
, wesanderson
, beyonce
, ochre
, ....# install.packages("usethis")library(usethis)use_course("dmac.netlify.com/lectures/lecture4b/exercise/exercise-4b.zip")
This work is licensed under a Creative Commons Attribution 4.0 International License.
Type of variable | How to map | Common errors |
---|---|---|
Categorical, qualitative | Category + count/proportion displayed, often as an area plot or with a small number of categories mapped to colour or symbol | Not including 0 on the count/proportion axis. Not ordering categories. |
Quantitative | Position along an axis | Displaying as a bar, especially when showing mean values. Mapping to colour. |
Date/Time | Time-ordered axis, different temporal resolutions to study long term trend, or seasonal patterns. Lines typically connect measurements to indicate temporal dependence | Time order corrupted |
Space | Conventional projections of the sphere, map aspect ratio | Wrong aspect ratio |
theme(aspect.ratio=1)
which sets the physical size of the plot to be the same, or in some ratio.)df <- tibble(x = runif(100), y = runif(100) * 10)ggplot(df, aes(x = x, y = y)) + geom_point() + coord_fixed()
ggplot(df, aes(x = x, y = y)) + geom_point() + coord_equal()
ggplot(df, aes(x = x, y = y)) + geom_point() + coord_fixed(ratio = 0.2)
ggplot(df, aes(x = x, y = y)) + geom_point() + theme(aspect.ratio = 1)
Interaction on a plot can help de-clutter it, by making labels only show on mouse over. Occasionally it can be useful to zoom into parts of the plot. Often it is useful to change the aspect ratio.
The plotly
package makes it easy to add interaction to ggplots.
library(plotly)p <- passengers %>% filter(type_of_flight == "INTL") %>% spread(key = bound, value = amount) %>% ggplot() + geom_point(aes(x = IN, y = OUT, label = airport)) + facet_wrap(~Year, ncol = 8) + coord_equal() + scale_x_continuous("Incoming passengers (mil)", breaks = seq(0, 8000000, 2000000), labels = seq(0, 8, 2)) + scale_y_continuous("Outgoing passengers (mil)", breaks = seq(0, 8000000, 2000000), labels = seq(0, 8, 2))ggplotly(p)
While the song is playing...
Draw a mental model / concept map of last lectures content on joins.
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 |