mpg | cyl | disp | hp |
|---|---|---|---|
21.0 | 6 | 160.0 | 110 |
21.0 | 6 | 160.0 | 110 |
22.8 | 4 | 108.0 | 93 |
21.4 | 6 | 258.0 | 110 |
18.7 | 8 | 360.0 | 175 |
18.1 | 6 | 225.0 | 105 |
14.3 | 8 | 360.0 | 245 |
24.4 | 4 | 146.7 | 62 |
22.8 | 4 | 140.8 | 95 |
19.2 | 6 | 167.6 | 123 |
Ex-8-1: Make tables with flextable
1 Exercise 1
Dataset: mtcars
- Load the
mtcarsdataset into R. - Use the
flextablepackage to create a table that displays the first 10 rows of the dataset formpg,cyl,disp, andhp. - Apply styles to the table to make
- the texts in the header bold using
bold() - the texts of the
cylcolumn center-aligned including thecylin the header usingalign() - the texts of the
hpcolumn colored red usingcolor() - the font size of the texts of the
mpg18 if the value ofdispis greater than 200 usingfontsize().
- the texts in the header bold using
Here is the finished table you are trying to make:
Code
# Create a simple flextable
ft <-
flextable(
mtcars[1:10, ],
col_keys = c("mpg", "cyl", "disp", "hp")
) %>%
bold(part = "header") %>%
align(j = 2, align = "center", part = "all") %>%
color(j = 4, color = "red")2 Exercise 2
Dataset: iris
- Load the
irisdataset. - Create a
flextablethat displays the first 10 rows of the dataset. - Apply conditional formatting:
- Change the background color of the cells in the
Sepal.Lengthcolumn where their values are greater than 5.0 in blue usingcolorusingbg(). - Draw orange borders at the bottom of the cells in the
Sepal.Widthcolumn if their values are less than 3.0 usingborder(). - Draw red borders at both sides of the cells in the
Sepal.Widthcolumn if their corresponding values ofPetal.Widthare less than 1.5 usingborder().
- Change the background color of the cells in the
Here is the finished table you are trying to make:
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3.0 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5.0 | 3.6 | 1.4 | 0.2 | setosa |
5.4 | 3.9 | 1.7 | 0.4 | setosa |
4.6 | 3.4 | 1.4 | 0.3 | setosa |
5.0 | 3.4 | 1.5 | 0.2 | setosa |
4.4 | 2.9 | 1.4 | 0.2 | setosa |
4.9 | 3.1 | 1.5 | 0.1 | setosa |
Code
# Create flextable for first 10 rows
ft <-
flextable(iris[1:10,]) %>%
# Apply conditional formatting to Sepal.Length column
bg(i = ~ Sepal.Length > 5, j = "Sepal.Length", bg = "blue") %>%
border(i = ~ Sepal.Width > 3, j = ~ Sepal.Width, border.bottom = officer::fp_border(color = "orange"))3 Exercise 3: Merging and Grouping Columns
Dataset: airquality
- Load the
airqualitydataset and filter the data so that you have only the first 10 days of May and June. - Create a flextable using selected columns:
Month,Day,Ozone,Wind. - Group the table by the
Monthcolumn and merge the cells of theMonthcolumn usingmerge_v(). - Draw a black border to separate May and June observations
- Draw a black line at the bottom of the table which was somehow lost in step 3 using
fix_border_issues(). - Add a caption to the table using
set_caption().
Here is the finished table you are trying to make:
Month | Day | Ozone | Wind |
|---|---|---|---|
5 | 1 | 41 | 7.4 |
2 | 36 | 8.0 | |
3 | 12 | 12.6 | |
4 | 18 | 11.5 | |
5 | 14.3 | ||
6 | 28 | 14.9 | |
7 | 23 | 8.6 | |
8 | 19 | 13.8 | |
9 | 8 | 20.1 | |
10 | 8.6 | ||
6 | 1 | 8.6 | |
2 | 9.7 | ||
3 | 16.1 | ||
4 | 9.2 | ||
5 | 8.6 | ||
6 | 14.3 | ||
7 | 29 | 9.7 | |
8 | 6.9 | ||
9 | 71 | 13.8 | |
10 | 39 | 11.5 |
Code
# Select columns
ft <-
airquality %>%
dplyr::filter(Month %in% c(5, 6) & Day <= 10) %>%
.[, c("Month", "Day", "Ozone", "Wind")] %>%
# Create flextable
flextable() %>%
# Merge Month column
merge_v(j = "Month") %>%
border(i = 10, border.bottom = officer::fp_border(color = "black")) %>%
fix_border_issues() %>%
# Add a caption
set_caption("Airquality Data Table with Grouped Month")