class: center, middle, inverse, title-slide .title[ # Effective Data Visualisation ] .subtitle[ ##
Introduction to Data Science ] .author[ ### University of Edinburgh ] .date[ ###
2024/2025 ] --- ## Let's have a discussion .pull-left[ <img src="img/pie-3d.jpg" width="100%" /> ] .pull-right[ <img src="w05-L10_files/figure-html/pie-to-bar-1.png" width="80%" style="display: block; margin: auto;" /> ] --- ## Let's have a discussion .pull-left[ <img src="w05-L10_files/figure-html/unnamed-chunk-1-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="w05-L10_files/figure-html/unnamed-chunk-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Let's have a discussion <img src="img/time-series-story.png" width="90%" style="display: block; margin: auto;" /> .footnote[ Credit: Angela Zoss and Eric Monson, Duke DVS ] --- ## Principles for effective visualizations - Think about the four respects: - Data - Mathematics - People - Computers - Logical order for a categorical variable - Ensure everything is readable (long categories names on the y-axis) - Are there other important variables? - Select clear and meaningful colours - Use meaningful title and labels - Be selective over redundant information --- ## Data In September 2019, YouGov survey asked 1,639 GB adults the following question: .pull-left[ > In hindsight, do you think Britain was right/wrong to vote to leave EU? > >- Right to leave >- Wrong to leave >- Don't know .small[ Source: [YouGov Survey Results](https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/x0msmggx08/YouGov%20-%20Brexit%20and%202019%20election.pdf), retrieved Oct 7, 2019 ] ] .pull-left[ ``` r brexit <- read_csv("data/brexit.csv") brexit %>% head(n = 2) ``` ``` ## # A tibble: 2 × 2 ## opinion region ## <chr> <chr> ## 1 Right rest_of_south ## 2 Wrong north ``` ``` r brexit %>% tail(n = 2) ``` ``` ## # A tibble: 2 × 2 ## opinion region ## <chr> <chr> ## 1 Wrong rest_of_south ## 2 Wrong scot ``` ] --- ## Visualise the data .panelset[ .panel[.panel-name[Plots] .pull-left[ <img src="w05-L10_files/figure-html/unnamed-chunk-5-1.png" width="80%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="w05-L10_files/figure-html/unnamed-chunk-6-1.png" width="80%" style="display: block; margin: auto;" /> ] ] .panel[.panel-name[Code] .pull-left[ ``` r ggplot(brexit, aes(x = opinion)) + geom_bar() ``` ] .pull-right[ ``` r ggplot(brexit, aes(x = region)) + geom_bar() ``` ] ] ] --- ## Question .pull-left[ <br> .question[ Are people's opinion about Brexit the same or different for each region? ] ``` r ggplot(brexit, aes(x = region, fill = opinion)) + geom_bar() ``` ] .pull-right[ <img src="w05-L10_files/figure-html/unnamed-chunk-7-1.png" width="80%" style="display: block; margin: auto;" /> ] Are we done? Can we do any better? --- ## Ensure text is readable .panelset[ .panel[.panel-name[Output] <img src="w05-L10_files/figure-html/unnamed-chunk-8-1.png" width="60%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code: Wrangling] ``` r brexit <- brexit %>% mutate( * region = fct_recode( region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot" ) ) ``` ] .panel[.panel-name[Code: Visualisation] ``` r *ggplot(brexit, aes(y = region, fill = opinion)) + geom_bar() ``` ] ] --- ## Is there a better ording .panelset[ .panel[.panel-name[Output] <img src="w05-L10_files/figure-html/unnamed-chunk-9-1.png" width="60%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code: Wrangling] ``` r brexit <- brexit %>% mutate( region = fct_recode( region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot" ), * region = fct_relevel( * region, * "Scotland", "North", "Midlands / Wales", "Rest of South", "London" * ), ) ``` ] .panel[.panel-name[Code: Visualisation] ``` r ggplot(brexit, aes(y = region, fill = opinion)) + geom_bar() ``` ] ] --- ## Faceting rather than stacked .panelset[ .panel[.panel-name[Output] <img src="w05-L10_files/figure-html/unnamed-chunk-10-1.png" width="70%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code: Wrangling] ``` r brexit <- brexit %>% mutate( region = fct_recode( region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot" ), region = fct_relevel( region, "Scotland", "North", "Midlands / Wales", "Rest of South", "London" ), ) ``` ] .panel[.panel-name[Code: Visualisation] ``` r *ggplot(brexit, aes(y = opinion, fill = opinion)) + geom_bar() + * facet_grid(~region) ``` ] ] --- ## Be selective with redundancy .panelset[ .panel[.panel-name[Output] <img src="w05-L10_files/figure-html/unnamed-chunk-11-1.png" width="70%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code: Wrangling] ``` r brexit <- brexit %>% mutate( region = fct_recode( region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot" ), region = fct_relevel( region, "Scotland", "North", "Midlands / Wales", "Rest of South", "London" ), ) ``` ] .panel[.panel-name[Code: Visualisation] ``` r ggplot(brexit, aes(y = opinion, fill = opinion)) + geom_bar() + facet_grid(~region) + labs( * y = NULL ) + * guides(fill = "none") #Removes legend ``` ] ] --- ## Use informative labels .panelset[ .panel[.panel-name[Output] <img src="w05-L10_files/figure-html/unnamed-chunk-12-1.png" width="70%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code: Wrangling] ``` r brexit <- brexit %>% mutate( region = fct_recode( region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot" ), region = fct_relevel( region, "Scotland", "North", "Midlands / Wales", "Rest of South", "London" ), ) ``` ] .panel[.panel-name[Code: Visualisation] ``` r ggplot(brexit, aes(y = opinion, fill = opinion)) + geom_bar() + facet_grid(~region) + labs( * title = "Was Britain right or wrong to leave the EU?", * subtitle = "YouGov Survey Results, 2-3 September 2019", * x = "Frequency", y = NULL ) + guides(fill = "none") ``` ] ] --- ## Selection of colours <img src="img/color-pallet.png" width="80%" style="display: block; margin: auto;" /> [colorbrewer2.org](https://colorbrewer2.org/) - Help with selecting a colour pallet --- ## Selection of colours .panelset[ .panel[.panel-name[Output] <img src="w05-L10_files/figure-html/unnamed-chunk-14-1.png" width="70%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code: Wrangling] ``` r brexit <- brexit %>% mutate( region = fct_recode( region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot" ), region = fct_relevel( region, "Scotland", "North", "Midlands / Wales", "Rest of South", "London" ), ) ``` ] .panel[.panel-name[Code: Visualisation] ``` r ggplot(brexit, aes(y = opinion, fill = opinion)) + geom_bar() + facet_grid(~region) + labs( title = "Was Britain right or wrong to leave the EU?", subtitle = "YouGov Survey Results, 2-3 September 2019", x = "Frequency", y = NULL ) + guides(fill = "none") + scale_fill_manual(values = c( * "Wrong" = "#ef8a62", * "Right" = "#67a9cf", * "Don't know" = "gray" )) ``` ] ] --- ## Iterate... .pull-left[ Reminder: .question[ Are people's opinion about Brexit the same or different for each region? ] - Has the data visualisation answered this question? - Is there anything that is unclear? - What further edits are needed? - Is this the only visualisation that can answer the question? - Can you create a better data visualisation? ] .pull-right[ <img src="w05-L10_files/figure-html/unnamed-chunk-15-1.png" width="100%" style="display: block; margin: auto;" /> ]