bonus_tables.Rmd
We don’t just do data analysis to do it. Usually, we need to share it with someone else. Whether that is a member of a lab, our boss, the general public, we need to be able to communicate results effectively. Most people probably do not want to look at data the way we’ve been looking for it.
The purpose of this notebook is to look at making tables prettier. We want to enable whoever we’re talking to to turn the data we have and the analysis we’ve done into insights.
Let us consider this case. You are designing a new trapping experiment out at the Portal, Arizona field site. You want to know what types of animal in- and exclosures are catching in the most animals
## # A tibble: 5 × 2
## # Groups: plot_type [5]
## plot_type n
## <chr> <int>
## 1 Control 506
## 2 Long-term Krat Exclosure 141
## 3 Rodent Exclosure 121
## 4 Short-term Krat Exclosure 209
## 5 Spectab exclosure 68
We’ve seen this before. It looks fine. It gets the message across. We can, however, make it look a little nicer:
table <- surveys %>%
na.omit() %>%
group_by(plot_type) %>%
filter(species_id == "NL") %>%
count()
knitr::kable(table )
plot_type | n |
---|---|
Control | 506 |
Long-term Krat Exclosure | 141 |
Rodent Exclosure | 121 |
Short-term Krat Exclosure | 209 |
Spectab exclosure | 68 |
We can make a few edits to display more intuitive labeling:
table <- table %>%
rename_at('plot_type', ~'Experimental Treatment')
table <- table %>%
rename_at('n', ~'Number Sampled')
knitr::kable(table)
Experimental Treatment | Number Sampled |
---|---|
Control | 506 |
Long-term Krat Exclosure | 141 |
Rodent Exclosure | 121 |
Short-term Krat Exclosure | 209 |
Spectab exclosure | 68 |
These are some viable options for small tables. Let’s think about bigger tables. Big things are hard to look at. Let’s get a good, but not overwhelming, amount of data.
data_for_table <- surveys %>%
na.omit() %>%
select(species_id, weight, year) %>%
group_by(year) %>%
summarize(mean_weight = mean(weight)) %>%
arrange()
data_for_table
## # A tibble: 26 × 2
## year mean_weight
## <dbl> <dbl>
## 1 1977 46.6
## 2 1978 66.2
## 3 1979 63.2
## 4 1980 64.6
## 5 1981 65.2
## 6 1982 52.7
## 7 1983 54.7
## 8 1984 50.7
## 9 1985 46.4
## 10 1986 54.9
## # ℹ 16 more rows
We can try a visual with kable.
data_for_table <- surveys %>%
na.omit() %>%
select(species_id, weight, year) %>%
group_by(year) %>%
summarize(mean_weight = mean(weight)) %>%
arrange()
knitr::kable(data_for_table)
year | mean_weight |
---|---|
1977 | 46.55512 |
1978 | 66.21739 |
1979 | 63.19056 |
1980 | 64.57789 |
1981 | 65.18014 |
1982 | 52.67731 |
1983 | 54.66106 |
1984 | 50.74800 |
1985 | 46.41154 |
1986 | 54.88319 |
1987 | 49.32500 |
1988 | 44.58118 |
1989 | 35.35225 |
1990 | 34.89904 |
1991 | 32.03571 |
1992 | 33.27897 |
1993 | 34.18859 |
1994 | 34.36813 |
1995 | 29.66978 |
1996 | 28.13416 |
1997 | 31.67637 |
1998 | 34.83116 |
1999 | 36.22316 |
2000 | 32.07805 |
2001 | 36.21897 |
2002 | 35.52647 |
This isn’t bad - it at least allows us to scroll and look at things.
The DT
package, however, allows us to look very comfortably
at these data. We can even search!
data_for_table <- surveys %>%
na.omit() %>%
select(species_id, weight, year) %>%
group_by(year) %>%
summarize(mean_weight = mean(weight)) %>%
arrange()
DT::datatable(data_for_table)
Even looking at surveys isn’t so painful with datatable. It’s a real memory hog, though, so don’t everyone do it at once.