banner



How To Bold A Color In R

Making tables as part of your information visualization strategy tin can be a striking or miss. For example, a table with too many numbers screams for a plot instead of a tabular array. Basically, challenge lies in using tables at the right time in right way. Luckily, we are in a much better position to brand beautiful tables using R. In this post, nosotros volition encounter 6 elementary tips to get started with making tables using the R package gt. The R package gt is developed and maintained by RStudio

With the gt packet, anyone can make wonderful-looking tables using the R programming language. The gt philosophy: we can construct a wide variety of useful tables with a cohesive ready of table parts. These include the table header, the stub, the column labels and spanner column labels, the tabular array body, and the table footer.

Let united states of america load the R packages needed.

library(tidyverse) library(gt) library(palmerpenguins)              

We will use the palmer penguin dataset to create a summary table of average species' body mass.

df <- penguins %>%    drop_na() %>%   group_by(species) %>%   summarize(mean_body_mass = hateful(body_mass_g))              

Out simple data frame looks like this and we would brand table from this dataframe.

df ## # A tibble: 3 10 2 ##   species   mean_body_mass ##   &lt;fct&gt;              &lt;dbl&gt; ## 1 Adelie             3706. ## 2 Chinstrap          3733. ## three Gentoo             5092.              

1. Simple table with gt

With gt(), nosotros can make a simple table by providing the dataframe to gt() function

# a simple gt table df %>% gt()              

And nosotros go a simple tabular array that looks like this.

Simple gt table
Unproblematic gt table

2. How to Add a Title to a Table with gt?

It volition be overnice to add together a title to our table. We can add title to a table in gt using tab_header() part every bit shown below. Within tab_header() function, we specify "title" argument with the title text we want.

df %>%    gt()%>%   tab_header(     championship = "Penguin Data Summary"   )              

Now our uncomplicated tabular array has a title.

How To Add Title to gt Table?
How To Add Title to gt Tabular array?

iii. How to Make the Title bold with markdown?

We tin make our title assuming using markdown inside tabe_header() part. Here we tin merely employ markdown syntax to exercise this.

df %>%    gt()%>%   tab_header(     title = md("**Penguin Information Summary**")   )              
How to make title bold gt table?
How to make title bold gt table?

4. How to Format Numbers in gt with fmt_number()?

A table with numbers can frequently exist hard read. One way to brand information technology piece of cake to read is to format the numbers. Here we use fmt_number() office to format the numbers with identify value and show just 2 decimal points. We specify which cavalcade to format.

df %>%    gt()%>%   tab_header(     title = medico("**Penguin Data Summary**")   ) %>%  fmt_number(     columns = vars(mean_body_mass),     decimals = 2   )              
How to add source note gt table?
How to add source note gt table?

5. How to Add together Source Note using gt table?

Nosotros tin add source note at the bottom of the tabular array using gt's tab_source_note() function. We specify the text nosotros want as source note using "source_note" as argument.

df %>%    gt()%>%   tab_header(     title = md("**Penguin Information Summary**")   ) %>%   fmt_number(     columns = vars(mean_body_mass),     decimals = 2   ) %>%   tab_source_note(     source_note = "Information Source: Dr. Kristen Gorman & Palmer Station, Antarctica LTER."   )              
How to add source note gt table?
How to add source note gt table?

half dozen. How to Highlight one or more rows with color in gt?

Adding colors to either select rows or columns can help highlight office of the table. With gt, we can selectively add colors to rows or columns using tab_style() function.

In the example beneath, nosotros specify colors using "style" argument. Nosotros specify the condition for highlighting rows using locations as shown beneath. Here nosotros highlight the row with mean trunk mass is greater than 4000 in greenish color.

df %>%    gt()%>%   tab_header(     title = md("**Penguin Information Summary**")   ) %>%  fmt_number(     columns = vars(mean_body_mass),     decimals = 2   ) %>%   tab_source_note(     source_note = "Information Source: Dr. Kristen Gorman & Palmer Station, Antarctica LTER."   ) %>%   tab_style(     style = listing(       cell_fill(color = "green"),       cell_text(weight = "bold")       ),     locations = cells_body(       columns = vars(species,mean_body_mass),       rows = mean_body_mass >= 4000)   )              
How to add colors to rows  gt table?
How to add together colors to rows gt table?

Source: https://datavizpyr.com/tips-to-make-beautiful-tables-with-gt/

Posted by: stevensprou1983.blogspot.com

0 Response to "How To Bold A Color In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel