Skip to contents

Flextable format for pivot_table

Usage

pivot_format(
  pivot,
  background = "#81A1C1",
  color = "#FFFFFF",
  border = "#FFFFFF",
  font_size = 11,
  font_name = NULL,
  labels = pivot_labels(),
  formatter = pivot_formatter(),
  zebra_style = c("classic", "stats", "none"),
  zebra_color = "#ECEFF4",
  drop_stats = FALSE,
  keep_data = TRUE
)

Arguments

pivot

A pivot_table object.

background

Background color for the header and column(s) containing row(s).

color

Text color for the header and column(s) containing row(s).

border

Border color (applies to all table).

font_size

Font size (applies to all table).

font_name

Font name (applies to all table).

labels

Custom labels for statistics, see pivot_labels.

formatter

Function to format content, see pivot_formatter.

zebra_style

Add zebra theme to table.

zebra_color

Color to use for zebra theme.

drop_stats

Drop the stats column, can be useful if have only one stat to show.

keep_data

Keep data as attribute, this can be useful to retrieve the data from which the table was formatted.

Value

a flextable object.

Examples

library(flexpivot)
library(magrittr)
data("nobel_laureates")

# With two variables
nobel_laureates %>%
  pivot_table("category", "gender") %>%
  pivot_format()

gender

category

Statistic

male

female

<missing>

Total

Chemistry

N

179

5

0

184

%

18.8%

0.5%

0%

19.4%

Col %

20.6%

9.3%

0%

19.4%

Row %

97.3%

2.7%

0%

100%

Economic Sciences

N

82

2

0

84

%

8.6%

0.2%

0%

8.8%

Col %

9.4%

3.7%

0%

8.8%

Row %

97.6%

2.4%

0%

100%

Literature

N

101

15

0

116

%

10.6%

1.6%

0%

12.2%

Col %

11.6%

27.8%

0%

12.2%

Row %

87.1%

12.9%

0%

100%

Peace

N

90

17

27

134

%

9.5%

1.8%

2.8%

14.1%

Col %

10.4%

31.5%

100%

14.1%

Row %

67.2%

12.7%

20.1%

100%

Physics

N

210

3

0

213

%

22.1%

0.3%

0%

22.4%

Col %

24.2%

5.6%

0%

22.4%

Row %

98.6%

1.4%

0%

100%

Physiology or Medicine

N

207

12

0

219

%

21.8%

1.3%

0%

23%

Col %

23.8%

22.2%

0%

23%

Row %

94.5%

5.5%

0%

100%

Total

N

869

54

27

950

%

91.5%

5.7%

2.8%

100%

Col %

100%

100%

100%

100%

Row %

91.5%

5.7%

2.8%

100%

# Only count nobel_laureates %>% pivot_table("category", "gender", stats = "n") %>% pivot_format(drop_stats = TRUE)

gender

category

male

female

<missing>

Total

Chemistry

179

5

0

184

Economic Sciences

82

2

0

84

Literature

101

15

0

116

Peace

90

17

27

134

Physics

210

3

0

213

Physiology or Medicine

207

12

0

219

Total

869

54

27

950

# Only percent nobel_laureates %>% pivot_table("category", "gender", stats = "p") %>% pivot_format()

gender

category

Statistic

male

female

<missing>

Total

Chemistry

%

18.8%

0.5%

0%

19.4%

Economic Sciences

%

8.6%

0.2%

0%

8.8%

Literature

%

10.6%

1.6%

0%

12.2%

Peace

%

9.5%

1.8%

2.8%

14.1%

Physics

%

22.1%

0.3%

0%

22.4%

Physiology or Medicine

%

21.8%

1.3%

0%

23%

Total

%

91.5%

5.7%

2.8%

100%

# Two variable as rows nobel_laureates %>% pivot_table(c("birth_continent", "gender"), "category", stats = "p") %>% pivot_format()

category

birth_continent

gender

Statistic

Chemistry

Economic Sciences

Literature

Peace

Physics

Physiology or Medicine

Total

Europe

male

%

10.6%

3%

7.5%

5%

11.8%

11.5%

49.5%

female

%

0.2%

0.1%

0.9%

0.5%

0.2%

0.5%

2.5%

<missing>

%

0%

0%

0%

0%

0%

0%

0%

Total

%

10.8%

3.2%

8.4%

5.6%

12%

12%

52%

Oceania

male

%

0.3%

0%

0%

0.2%

0.2%

0.7%

1.5%

female

%

0%

0%

0%

0%

0%

0.1%

0.1%

<missing>

%

0%

0%

0%

0%

0%

0%

0%

Total

%

0.3%

0%

0%

0.2%

0.2%

0.8%

1.6%

North America

male

%

6.1%

5.3%

1.4%

2%

7.8%

7.9%

30.4%

female

%

0.1%

0.1%

0.3%

0.4%

0.1%

0.5%

1.6%

<missing>

%

0%

0%

0%

0%

0%

0%

0%

Total

%

6.2%

5.4%

1.7%

2.4%

7.9%

8.4%

32%

Africa

male

%

0.2%

0%

0.5%

1%

0.2%

0.3%

2.3%

female

%

0.1%

0%

0.1%

0.3%

0%

0%

0.5%

<missing>

%

0%

0%

0%

0%

0%

0%

0%

Total

%

0.3%

0%

0.6%

1.4%

0.2%

0.3%

2.8%

Asia

male

%

1.6%

0.3%

0.9%

0.8%

2.1%

0.9%

6.7%

female

%

0.1%

0%

0.1%

0.5%

0%

0.1%

0.8%

<missing>

%

0%

0%

0%

0%

0%

0%

0%

Total

%

1.7%

0.3%

1%

1.4%

2.1%

1%

7.6%

South America

male

%

0%

0%

0.3%

0.3%

0%

0.4%

1%

female

%

0%

0%

0.1%

0%

0%

0%

0.1%

<missing>

%

0%

0%

0%

0%

0%

0%

0%

Total

%

0%

0%

0.4%

0.3%

0%

0.4%

1.2%

<missing>

male

%

0%

0%

0%

0%

0%

0%

0%

female

%

0%

0%

0%

0%

0%

0%

0%

<missing>

%

0%

0%

0%

2.8%

0%

0%

2.8%

Total

%

0%

0%

0%

2.8%

0%

0%

2.8%

Total

male

%

18.8%

8.6%

10.6%

9.5%

22.1%

21.8%

91.5%

female

%

0.5%

0.2%

1.6%

1.8%

0.3%

1.3%

5.7%

<missing>

%

0%

0%

0%

2.8%

0%

0%

2.8%

Total

%

19.4%

8.8%

12.2%

14.1%

22.4%

23%

100%

# Without cols pivot_format(pivot_table(nobel_laureates, rows = "category"))

category

N

%

Chemistry

184

19.4%

Economic Sciences

84

8.8%

Literature

116

12.2%

Peace

134

14.1%

Physics

213

22.4%

Physiology or Medicine

219

23%

Total

950

100%

pivot_format(pivot_table(nobel_laureates, rows = c("category", "gender")))

category

gender

N

%

Chemistry

male

179

18.8%

female

5

0.5%

<missing>

0

0%

Total

184

19.4%

Economic Sciences

male

82

8.6%

female

2

0.2%

<missing>

0

0%

Total

84

8.8%

Literature

male

101

10.6%

female

15

1.6%

<missing>

0

0%

Total

116

12.2%

Peace

male

90

9.5%

female

17

1.8%

<missing>

27

2.8%

Total

134

14.1%

Physics

male

210

22.1%

female

3

0.3%

<missing>

0

0%

Total

213

22.4%

Physiology or Medicine

male

207

21.8%

female

12

1.3%

<missing>

0

0%

Total

219

23%

Total

male

869

91.5%

female

54

5.7%

<missing>

27

2.8%

Total

950

100%

# Customize nobel_laureates %>% pivot_table("category") %>% pivot_format( background = "#A3BE8C", labels = pivot_labels(n = "Count", p = "Percentage") )

category

Count

Percentage

Chemistry

184

19.4%

Economic Sciences

84

8.8%

Literature

116

12.2%

Peace

134

14.1%

Physics

213

22.4%

Physiology or Medicine

219

23%

Total

950

100%