How to make row height smaller?
t<-mpg %>% tab_cells(hwy,displ) %>%
tab_rows(manufacturer) %>%
tab_stat_mean() %>%
tab_pivot() %>%
set_caption("Car Stats") %>% as_huxtable()
t %>%
set_col_width(1,.5) %>%
set_row_height(.2) %>%
theme_article()
#Total | ||||
---|---|---|---|---|
manufacturer | audi | hwy | Mean | 26.4 |
displ | 2.54 | |||
chevrolet | hwy | 21.9 | ||
displ | 5.06 | |||
dodge | hwy | 18 | ||
displ | 4.34 |
mpg %>% tab_cells(hwy,displ) %>%
tab_rows(manufacturer) %>%
tab_stat_mean() %>%
tab_pivot() %>%
set_caption("Car Stats") %>% as_huxtable() %>%
set_col_width(1,.1) %>%
theme_article()->t
height(t)<-.2
t
#Total | ||||
---|---|---|---|---|
manufacturer | audi | hwy | Mean | 26.4 |
displ | 2.54 | |||
chevrolet | hwy | 21.9 | ||
displ | 5.06 | |||
dodge | hwy | 18 | ||
displ | 4.34 |
mpg %>% tab_cells(hwy,displ) %>%
tab_rows(manufacturer) %>%
tab_stat_mean() %>%
tab_pivot() %>%
set_caption("Car Stats") %>% as_huxtable() %>%
set_col_width(1,0.1) ->t
height(t)<-.1
t
#Total | ||||
---|---|---|---|---|
manufacturer | audi | hwy | Mean | 26.4 |
displ | 2.54 | |||
chevrolet | hwy | 21.9 | ||
displ | 5.06 | |||
dodge | hwy | 18 | ||
displ | 4.34 |
Theme has nothing to do with this…. Use padding parameters
mpg %>% tab_cells(hwy,displ) %>%
tab_rows(manufacturer) %>%
tab_stat_mean() %>%
tab_pivot() %>%
set_caption("Car Stats") %>% as_huxtable() %>%
theme_blue() %>%
set_col_width(1,2.3) ->t
t %>%
set_top_padding(.2) %>%
set_bottom_padding(.2) %>% set_caption("This works...")
#Total | ||||
---|---|---|---|---|
manufacturer | audi | hwy | Mean | 26.4 |
displ | 2.54 | |||
chevrolet | hwy | 21.9 | ||
displ | 5.06 | |||
dodge | hwy | 18 | ||
displ | 4.34 |
Conclusion: make a custom function that defines your table structure, like this:
tset<-function(x){as_huxtable(x) %>% set_number_format(1) %>%
set_bold(row = 1, col = everywhere) %>%
set_bottom_border(row = 1, col = everywhere) %>%
set_bottom_border(row = dim(t)[1], col = everywhere) %>%
set_top_padding(.2) %>%
set_bottom_padding(.2) %>%
theme_article() }
mpg %>% tab_cells(hwy,displ) %>%
tab_rows(manufacturer) %>%
tab_stat_mean() %>%
tab_pivot() %>%
set_caption("Car Stats") ->t
tset(t) %>%
set_col_width(1,1.5) %>% # column 1 is to be "1.5" units wide
set_caption("Used function with `t` ")
#Total | ||||
---|---|---|---|---|
manufacturer | audi | hwy | Mean | 26.4 |
displ | 2.5 | |||
chevrolet | hwy | 21.9 | ||
displ | 5.1 | |||
dodge | hwy | 18.0 | ||
displ | 4.3 |
tset(t) %>%
set_col_width(1,.2) %>% # set additional parameters beyond the tset() function
set_col_width(2,.3) %>%
huxtable::theme_striped() %>%
set_caption("Used function with `t` with narrower column width ")
#Total | ||||
---|---|---|---|---|
manufacturer | audi | hwy | Mean | 26.4 |
displ | 2.5 | |||
chevrolet | hwy | 21.9 | ||
displ | 5.1 | |||
dodge | hwy | 18.0 | ||
displ | 4.3 |