library(tidyverse) # Sine qua non
library(mosaic) # Out all-in-one package
library(ggformula) # Graphing package
library(skimr) # Looking at Data
library(janitor) # Clean the data
library(naniar) # Handle missing data
library(visdat) # Visualise missing data
library(tinytable) # Printing Static Tables for our data
library(DT) # Interactive Tables for our data
library(crosstable) # Multiple variable summaries
library(marquee) # For Annotations with Fonts
library(ggrepel) # Repel overlapping text labels in ggplot2
Counts
How Many of this and that?
“No matter what happens in life, be good to people. Being good to people is a wonderful legacy to leave behind.”
— Taylor Swift
1 Setting up R Packages
Plot Fonts and Theme
Show the Code
library(systemfonts)
library(showtext)
## Clean the slate
systemfonts::clear_local_fonts()
systemfonts::clear_registry()
##
showtext_opts(dpi = 96) # set DPI for showtext
sysfonts::font_add(
family = "Alegreya",
regular = "../../../../../../fonts/Alegreya-Regular.ttf",
bold = "../../../../../../fonts/Alegreya-Bold.ttf",
italic = "../../../../../../fonts/Alegreya-Italic.ttf",
bolditalic = "../../../../../../fonts/Alegreya-BoldItalic.ttf"
)
sysfonts::font_add(
family = "Roboto Condensed",
regular = "../../../../../../fonts/RobotoCondensed-Regular.ttf",
bold = "../../../../../../fonts/RobotoCondensed-Bold.ttf",
italic = "../../../../../../fonts/RobotoCondensed-Italic.ttf",
bolditalic = "../../../../../../fonts/RobotoCondensed-BoldItalic.ttf"
)
showtext_auto(enable = TRUE) # enable showtext
##
theme_custom <- function() {
theme_bw(base_size = 10) +
theme_sub_axis(
title = element_text(
family = "Roboto Condensed",
size = 8
),
text = element_text(
family = "Roboto Condensed",
size = 6
)
) +
theme_sub_legend(
text = element_text(
family = "Roboto Condensed",
size = 6
),
title = element_text(
family = "Alegreya",
size = 8
)
) +
theme_sub_plot(
title = element_text(
family = "Alegreya",
size = 14, face = "bold"
),
title.position = "plot",
subtitle = element_text(
family = "Alegreya",
size = 10
),
caption = element_text(
family = "Alegreya",
size = 6
),
caption.position = "plot"
)
}
## Use available fonts in ggplot text geoms too!
ggplot2::update_geom_defaults(geom = "text", new = list(
family = "Roboto Condensed",
face = "plain",
size = 3.5,
color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "label", new = list(
family = "Roboto Condensed",
face = "plain",
size = 3.5,
color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "marquee", new = list(
family = "Roboto Condensed",
face = "plain",
size = 3.5,
color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "text_repel", new = list(
family = "Roboto Condensed",
face = "plain",
size = 3.5,
color = "#2b2b2b"
))
ggplot2::update_geom_defaults(geom = "label_repel", new = list(
family = "Roboto Condensed",
face = "plain",
size = 3.5,
color = "#2b2b2b"
))
## Set the theme
ggplot2::theme_set(new = theme_custom())
## tinytable options
options("tinytable_tt_digits" = 2)
options("tinytable_format_num_fmt" = "significant_cell")
options(tinytable_html_mathjax = TRUE)
## Set defaults for flextable
flextable::set_flextable_defaults(font.family = "Roboto Condensed")
2 What graphs will we see today?
Variable #1 | Variable #2 | Chart Names | Chart Shape | |
---|---|---|---|---|
Qual | None | Bar Chart |
3 What kind of Data Variables will we choose?
No | Pronoun | Answer | Variable/Scale | Example | What Operations? |
---|---|---|---|---|---|
3 | How, What Kind, What Sort | A Manner / Method, Type or Attribute from a list, with list items in some " order" ( e.g. good, better, improved, best..) | Qualitative/Ordinal | Socioeconomic status (Low income, Middle income, High income),Education level (HighSchool, BS, MS, PhD),Satisfaction rating(Very much Dislike, Dislike, Neutral, Like, Very Much Like) | Median,Percentile |
3.1 Inspiration: Column Chart
How much does the (financial) capital of a country contribute to its GDP? Which would be India’s city? What would be the reduction in percentage? And these Germans are crazy. (Toc, toc, toc, toc!)
Note how the axis variable that defines the bar locations
is a …Qual variable!
3.2 ggformula and mosaic API
- Recall the API: the programming interface to each of
mosaic
,ggformula
, andggplot
. - As stated earlier,
mosaic
andggformula
have a very similar, and intuitive, interface.
Note the standard method for all commands from the mosaic and ggformula packages: goal( y ~ x | z, data = _____)
With mosaic, one can create a statistical correlation test between two variables as: cor_test(y ~ x, data = ______ )
With ggformula, one can create any graph/chart using: gf_***(y ~ x | z, data = _____)
- In practice, we often use: dataframe %>% gf_***(y ~ x | z)
which has cool benefits such as “autocompletion” of variable names. - The ” *** ” indicates what kind of graph you desire: histogram, bar, scatter, density; - The “——-” is the name of your dataset that you want to plot with.
3.3 ggplot API
ggplot
command template
ggplot(data = ---, mapping = aes(x = ---, y = ---)) + geom_----()
- ” —- ” is meant to imply text you supply. e.g. function names, data frame names, variable names.
- It is helpful to see the argument mapping, above.
- In practice, rather than typing the formal arguments…
-
ggplot
code is typically shorthanded to this:
dataframe %>% ggplot(aes(xvar, yvar)) + geom_----()
- Note the change from %>%
to +
when adding a geom. Sigh.
4 Bar Charts and Histograms
4.1 Bar Charts and Histograms: Similar but Different
- Bar Charts show counts of observations with respect to a Qualitative variable.
- For instance, a shop inventory with shirt-sizes.
- Each bar has a height proportional to the count per shirt-size, in this example.
- Although Histograms may look similar to Bar Charts, the two are different.
- First, histograms show continuous Quant data.
- By contrast, bar charts show categorical data, such as shirt-sizes, or apples, bananas, carrots, etc.
- Visually speaking, histograms do not usually show spaces between bars because these are continuous values,
- While column charts must show spaces to separate each category.
4.2 How do Bar Chart(s) Work?
- Bar are used to show “counts” and “tallies” with respect to Qual variables: they answer the question
How Many?
. - For instance, in a survey, how many people vs Gender?
- In a Target Audience survey on Weekly Consumption, how many low, medium, or high expenditure people?
- Each Qual variable potentially has many levels as we saw in the Nature of Data.
- In Weekly Consumption,
low
,medium
andhigh
were levels for the Qual variableExpenditure
. - Bar charts perform internal counts for each level of the Qual variable under consideration.
- The Bar Plot is then a set of disjoint bars representing these counts; see the icon above, and then that for histograms!!
- The X-axis is the set of levels in the Qual variable, and the Y-axis represents the counts for each level.
5 Case Study-1: Chicago Taxi Rides dataset
5.1 Read Data
We will first look at at a dataset that speaks about taxi rides in Chicago in the year 2022. This is available on Vincent Arel-Bundock’s superb repository of datasets. Let us read into R directly from the website.
Show the Code
taxi <- read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/modeldata/taxi.csv")
taxi_modified <- taxi %>%
naniar::replace_with_na_all(condition = ~ .x %in% common_na_strings) %>%
naniar::replace_with_na_all(condition = ~ .x %in% common_na_numbers) %>%
janitor::clean_names(case = "snake") %>%
janitor::remove_empty()
taxi_modified
The data has automatically been read into the webr
session, so you can continue on to the next code chunk!
5.2 Examine the Data
As per our Workflow, we will look at the data using all the three methods we have seen.
dplyr::glimpse(taxi)
Rows: 10,000
Columns: 8
$ rownames <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18…
$ tip <chr> "yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes"…
$ distance <dbl> 17.19, 0.88, 18.11, 20.70, 12.23, 0.94, 17.47, 17.67, 1.85, 1…
$ company <chr> "Chicago Independents", "City Service", "other", "Chicago Ind…
$ local <chr> "no", "yes", "no", "no", "no", "yes", "no", "no", "no", "no",…
$ dow <chr> "Thu", "Thu", "Mon", "Mon", "Sun", "Sat", "Fri", "Sun", "Fri"…
$ month <chr> "Feb", "Mar", "Feb", "Apr", "Mar", "Apr", "Mar", "Jan", "Apr"…
$ hour <dbl> 16, 8, 18, 8, 21, 23, 12, 6, 12, 14, 18, 11, 12, 19, 17, 13, …
skimr::skim(taxi_modified)
Name | taxi_modified |
Number of rows | 10000 |
Number of columns | 8 |
_______________________ | |
Column type frequency: | |
character | 5 |
numeric | 3 |
________________________ | |
Group variables | None |
Variable type: character
skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
---|---|---|---|---|---|---|---|
tip | 0 | 1 | 2 | 3 | 0 | 2 | 0 |
company | 0 | 1 | 5 | 28 | 0 | 7 | 0 |
local | 0 | 1 | 2 | 3 | 0 | 2 | 0 |
dow | 0 | 1 | 3 | 3 | 0 | 7 | 0 |
month | 0 | 1 | 3 | 3 | 0 | 4 | 0 |
Variable type: numeric
skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
---|---|---|---|---|---|---|---|---|---|---|
rownames | 4 | 1 | 5001.48 | 2885.78 | 1 | 2502.75 | 5001.50 | 7500.25 | 10000.0 | ▇▇▇▇▇ |
distance | 0 | 1 | 6.22 | 7.38 | 0 | 0.94 | 1.78 | 15.56 | 42.3 | ▇▁▂▁▁ |
hour | 0 | 1 | 14.18 | 4.36 | 0 | 11.00 | 15.00 | 18.00 | 23.0 | ▁▃▅▇▃ |
mosaic::inspect(taxi_modified)
categorical variables:
name class levels n missing
1 tip character 2 10000 0
2 company character 7 10000 0
3 local character 2 10000 0
4 dow character 7 10000 0
5 month character 4 10000 0
distribution
1 yes (92.1%), no (7.9%)
2 other (27.1%) ...
3 no (81.2%), yes (18.8%)
4 Thu (19.6%), Wed (17.5%), Tue (16.3%) ...
5 Apr (31.8%), Mar (31.4%), Feb (20.4%) ...
quantitative variables:
name class min Q1 median Q3 max mean
1 rownames numeric 1 2502.75 5001.50 7500.2500 10000.0 5001.477591
2 distance numeric 0 0.94 1.78 15.5625 42.3 6.224144
3 hour numeric 0 11.00 15.00 18.0000 23.0 14.177300
sd n missing
1 2885.779856 9996 4
2 7.381397 10000 0
3 4.359904 10000 0
5.3 Data Dictionary
-
distance
: Continuous Quant variable, the distance of the trip in miles.
-
tip
: Yes/No type Qual variable, whether a tip was given or not. -
company
: 7 levels, the cab company that was used for the ride. -
local
: 2 levels, whether the trip was local or not. -
hour
: 24 levels, the hour of the day when the trip started. -
dow
: 7 levels, the day of the week. -
month
: 12 levels, the month of the year.
5.4 Business Insights on Examining the taxi
dataset
- This is a large dataset (10K rows), 8 columns/variables.
- There are several Qualitative variables:
tip
(2),company
(7) andlocal
(2),dow
(7), andmonth
(12). These have levels as shown in the parenthesis. - Note that
hour
despite being a discrete/numerical variable, it can be treated as a Categorical variable too. -
distance
is Quantitative. - There are no missing values for any variable, all are complete with 10K entries.
5.5 Data Munging
We will convert the tip
, company
, dow
, local
, hour
, and month
variables into factors beforehand.
Show the Code
## Convert `dow`, `local`, `month`, and `hour` into ordered factors
taxi_modified <- taxi_modified %>%
dplyr::mutate(
## Variable "tip"
tip = base::factor(tip,
levels = c("yes", "no"),
labels = c("yes", "no"),
ordered = TRUE
),
## Variable "company"
company = base::factor(company), # Any order is OK.
## Variable "dow"
dow = base::factor(dow,
levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
labels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
ordered = TRUE
),
## Variable "local"
local = base::factor(local,
levels = c("yes", "no"),
labels = c("yes", "no"),
ordered = TRUE
),
## Variable "month"
month = base::factor(month,
levels = c("Jan", "Feb", "Mar", "Apr"),
labels = c("Jan", "Feb", "Mar", "Apr"),
ordered = TRUE
),
## Variable "hour"
hour = base::factor(hour,
levels = c(0:23), labels = c(0:23),
ordered = TRUE
)
) %>%
dplyr::relocate(where(is.factor), .after = rownames) # Move all factors to the left
taxi_modified %>% glimpse()
Rows: 10,000
Columns: 8
$ rownames <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18…
$ tip <ord> yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, y…
$ company <fct> Chicago Independents, City Service, other, Chicago Independen…
$ local <ord> no, yes, no, no, no, yes, no, no, no, no, no, no, no, yes, no…
$ dow <ord> Thu, Thu, Mon, Mon, Sun, Sat, Fri, Sun, Fri, Tue, Tue, Sun, W…
$ month <ord> Feb, Mar, Feb, Apr, Mar, Apr, Mar, Jan, Apr, Mar, Mar, Apr, A…
$ hour <ord> 16, 8, 18, 8, 21, 23, 12, 6, 12, 14, 18, 11, 12, 19, 17, 13, …
$ distance <dbl> 17.19, 0.88, 18.11, 20.70, 12.23, 0.94, 17.47, 17.67, 1.85, 1…
Looks clean and good.
5.6 Hypothesis and Research Questions
- It is a good practice in Exploratory Data Analysis to surmise the Experiment that lead to the gathering of this dataset
- And what the target variable might be.
- This is the variable that is to be explained, or predicted, or modelled.
- The other variables are explanatory variables, or predictor variables.
- The target variable for an experiment that resulted in this data might be the
tip
variable - Since that looks like a response, or an outcome.
- It is a binary i.e. Yes/No type Qual variable.
- We will concentrate on the
tip
target variable to ask questions about the data, and then plot the answers to these questions.
5.7 Research Questions
- Do more people
tip
than not? - Does a
tip
depend upon whether the trip islocal
or not? - Do some cab
company
-ies get more tips than others? - And does a
tip
depend upon thedistance
,hour
of day, anddow
andmonth
?
Try and think of more Questions!
6 Plotting Barcharts
Let’s plot some bar graphs: recall that for bar charts, we need to choose Qual variables to count with! In each case, we will state a Hypothesis/Question and try to answer it with a chart.
6.1 Question-1: Do more people tip
than not?
- Far more people do
tip
than not. Which is nice. - (Future) The counts of
tip
are very imbalanced and if we are to setup a model for that (logistic regression) we would need to very carefully subset the data fortraining
andtesting
our model.
6.2 Question-2: Does the tip
depend upon whether the trip is local
or not?
ggplot2::theme_set(new = theme_custom())
## Showing "per capita" percentages
taxi_modified %>%
gf_bar(~local,
fill = ~tip,
position = "fill"
) %>%
gf_labs(
title = "Plot 2C: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!"
) %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
ggplot2::theme_set(new = theme_custom())
## Showing "per capita" percentages
## Better labelling of Y-axis
taxi_modified %>%
gf_props(~local,
fill = ~tip,
position = "fill"
) %>%
gf_labs(
title = "Plot 2D: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!"
) %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
ggplot2::theme_set(new = theme_custom())
taxi_modified %>%
ggplot() +
geom_bar(aes(x = local, fill = tip), position = "dodge") +
labs(title = "Plot 2A:Dodged Bar Chart") +
scale_fill_brewer(palette = "Set1")
##
taxi_modified %>%
ggplot() +
geom_bar(aes(x = local, fill = tip), position = "stack") +
labs(
title = "Plot 2B: Stacked Bar Chart",
subtitle = "Can we spot per group differences in proportions??"
) +
scale_fill_brewer(palette = "Set1")
## Showing "per capita" percentages
taxi_modified %>%
ggplot() +
geom_bar(aes(x = local, fill = tip), position = "fill") +
labs(title = "Plot 2C: Filled Bar Chart", subtitle = "Shows Per group differences in Proportions!") +
scale_fill_brewer(palette = "Set1")
## Showing "per capita" percentages
## Better labelling of Y-axis
taxi_modified %>%
ggplot() +
geom_bar(aes(x = local, fill = tip), position = "fill") +
labs(
title = "Plot 2D: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!",
y = "Proportion"
) +
scale_fill_brewer(palette = "Set1")
- Counting the frequency of
tip
bylocal
gives us grouped counts, but we cannot tell the percentage per group (local or not) of those who tip and those who do not. - We need per-group percentages because the number of
local
trips are not balanced - With ggformula, we tried bar charts with
position = stack
, but finally it is theposition = fill
that works best. - We see that the percentage of tippers is somewhat higher with people who make non-local trips. Not surprising.
6.3 Question-3: Do some cab company
-ies get more tips
than others?
ggplot2::theme_set(new = theme_custom())
taxi_modified %>%
gf_bar(~company, fill = ~tip, position = "stack") %>%
gf_labs(
title = "Plot 3B: Stacked Bar Chart",
subtitle = "Can we spot per group differences in proportions??"
) %>%
gf_theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
ggplot2::theme_set(new = theme_custom())
## Showing "per capita" percentages
taxi_modified %>%
gf_percents(~company, fill = ~tip, position = "fill") %>%
gf_labs(
title = "Plot 3C: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!"
) %>%
gf_theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
ggplot2::theme_set(new = theme_custom())
## Showing "per capita" percentages
## Better labelling of Y-axis
taxi_modified %>%
gf_props(~company, fill = ~tip, position = "fill") %>%
gf_labs(
title = "Plot 3D: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!"
) %>%
gf_theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
ggplot2::theme_set(new = theme_custom())
taxi_modified %>%
ggplot() +
geom_bar(aes(x = company, fill = tip), position = "dodge") +
labs(title = "Plot 3A: Dodged Bar Chart") +
theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) +
scale_fill_brewer(palette = "Set1")
##
taxi_modified %>%
ggplot() +
geom_bar(aes(x = company, fill = tip), position = "stack") +
labs(
title = "Plot 3B: Stacked Bar Chart",
subtitle = "Can we spot per group differences in proportions??"
) +
theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) +
scale_fill_brewer(palette = "Set1")
## Showing "per capita" percentages
taxi_modified %>%
ggplot() +
geom_bar(aes(x = company, fill = tip), position = "fill") +
labs(
title = "Plot 3C: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!"
) +
theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) +
scale_fill_brewer(palette = "Set1")
## Showing "per capita" percentages
## Better labelling of Y-axis
taxi_modified %>%
ggplot() +
geom_bar(aes(x = company, fill = tip), position = "fill") +
labs(
title = "Plot 3D: Filled Bar Chart",
subtitle = "Shows Per group differences in Proportions!",
y = "Proportions"
) +
theme(theme(axis.text.x = element_text(size = 6, angle = 45, hjust = 1))) +
scale_fill_brewer(palette = "Set1")
- Using
stack
-ed,dodge
-ed, andfill
-ed in ggformula in bar plots gives us different ways of looking at the sets of counts; -
fill
: gives us a per-group proportion of another Qual variable for a chosen Qual variable. This chart view is useful in Inference for Proportions; - Most cab
company
-ies have similar usage, if you neglect theother
category ofcompany
; - Does seem that of all the
company
-ies,tips
are not so good for theFlash Cab
company. A driver issue? Or are the cars too old? Or don’t they offer service everywhere?
6.4 Question-4: Does a tip
depend upon the distance
, hour
of day, and dow
and month
?
ggplot2::theme_set(new = theme_custom())
## This may be too busy a graph...
gf_bar(~ dow | hour, fill = ~tip, data = taxi_modified) %>%
gf_labs(
title = "Plot 4E: Counts of Tips by Hour and Day of Week",
subtitle = "Is this plot arrangement easy to grasp?"
) %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
ggplot2::theme_set(new = theme_custom())
gf_bar(~hour, fill = ~tip, data = taxi_modified) %>%
gf_labs(title = "Plot 4A: Counts of Tips by Hour") %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
##
ggplot(taxi_modified) +
geom_bar(aes(x = dow, fill = tip)) +
labs(title = "Plot 4B: Counts of Tips by Day of Week") +
scale_fill_brewer(palette = "Set1")
##
ggplot(taxi_modified) +
geom_bar(aes(x = month, fill = tip)) +
labs(title = "Plot 4C: Counts of Tips by Month") +
scale_fill_brewer(palette = "Set1")
##
ggplot(taxi_modified) +
geom_bar(aes(x = month, fill = tip)) +
facet_wrap(~dow) +
labs(title = "Plot 4D: Counts of Tips by Day of Week and Month") +
scale_fill_brewer(palette = "Set1")
##
ggplot(taxi_modified) +
geom_bar(aes(x = dow, fill = tip)) +
facet_wrap(~hour) +
labs(
title = "Plot 4E: Counts of Tips by Hour and Day of Week",
subtitle = "Is this plot arrangement easy to grasp?"
) +
scale_fill_brewer(palette = "Set1")
##
ggplot(taxi_modified) +
geom_bar(aes(x = hour, fill = tip)) +
facet_wrap(~dow) +
labs(
title = "Plot 4F: Counts of Tips by Hour and Day of Week",
subtitle = "Swapped the Facets"
) +
scale_fill_brewer(palette = "Set1")
- Note: We were using
fill = ~ tip
here! Why is that a good idea? -
tips
vshour
: There are always more people whotip
than those who do not. Of course there are fewer trips during the early morning hours and the late night hours, based on the very small bar-pairs we see at those times -
tips
vsdow
: Except for Sunday, thetip
count patterns (Yes/No) look similar across all days. -
tips
vsmonth
: We have data for 4 months only. Again, thetip
count patterns (Yes/No) look similar across all months. Perhaps slightly fewer trips in Jan, when it is cold in Chicago and people may not go out much. -
tips
vsdow
vsmonth
: Very similar counts fortips
(Yes/No) across day-of-week and month.
7 Bar Plot Extras
7.1 gf-bar and gf-col
- Note also that
gf_bar/geom_bar
takes only ONE variable (for the x-axis) - Whereas
gf_col/geom_col
needs both X and Y variables since it simply plots columns. - Both are useful!
- We have already seen
gf_props
in our two case studies above. - Also check out
gf_percents
!
7.2 Proportions and Percentages
ggplot2::theme_set(new = theme_custom())
gf_percents(~substance,
data = mosaicData::HELPrct, fill = ~sex,
position = "dodge"
) %>%
gf_refine(
scale_y_continuous(
labels = scales::label_percent(scale = 1)
)
) %>%
gf_labs(title = "Plotting Percentages using gf_percents") %>%
gf_refine(scale_fill_brewer(palette = "Set1"))
8 Are the Differences in Proportion Significant?
- When we see situations such as this, where data has one or more Qual variables that are binary(Yes/No)..
- We are always interested in whether these proportions of Yes/No are really different
- Or if we are just seeing the result of random chance.
- This is usually mechanized by a Stat Test called a Single Proportion Test
- Or, when we have more than one, a Multiple Proportion Test.
9 Your Turn
- Click on the Dataset Icon, and unzip that archive. Try to make Bar plots with each of them, using one or more Qual variables. Datasets
- A dataset from calmcode.io https://calmcode.io/datasets.html
- AiRbnb Price Data on the French Riviera.
- Apartment price vs ground living area.
- Fertility: This rather large and interesting Fertility related dataset from https://vincentarelbundock.github.io/Rdatasets/csv/AER/Fertility.csv
- Songs by Kishore Kumar: https://sunilslists.com/hindi-songs/luminaries-hindi-songs/kishore-kumar-songs-all