1. 程式人生 > 實用技巧 >千牛包表包下載_探索R中表包的gt語法

千牛包表包下載_探索R中表包的gt語法

千牛包表包下載

I love migrittr’s forward-pipe %>% operator. IMO, it is just the one of the most fitting, useful and natural, almost reflex like operator in all programming. In the past, it has propelled the tidyverse to data wrangling superstardom and it is now venturing into the task of making tables dramatically easy.

我喜歡migrittr的前管%>%運算子。 IMO,它是所有程式設計中最合適,最有用,最自然,幾乎像反射一樣的運算子之一。 過去,它已將tidyverse推向了爭奪超級明星的資料,現在正冒險使表變得非常容易的任務。

The gt package is the latest ambitious entry to make tables in R more accessible, modifiable and reproducible. It is aimed at being the ggplot2 for tables (in Hadley’s words, not mine) and given the magnanimous impact that the former had on the R viz, the package comes with immense pressure of its shoulders.

gt軟體包是最新的雄心勃勃的條目,它可以使R中的表更易於訪問,修改和再現。 它的目的是作為桌子的ggplot2(用Hadley的話說,不是我的),並且鑑於前者對R viz產生的巨大影響,該包裝的肩膀承受著巨大的壓力。

Here’s an illustration of the architecture of the gt package —

這是gt包的體系結構的說明-

Image for post
The Grammar Of Tables Layout. Source — GT Documentation, R Studio
表佈局語法。 來源— GT Studio,R Studio

We will be using the palmer penguins data, an alternative to the now infamous iris dataset. You can download the data here!

我們將使用帕爾默企鵝資料,這是現在臭名昭著的虹膜資料集的替代方法。 您可以在此處下載資料!

Post performing basic tidyverse verbs to filter and group the data in the desired shape, we invoke the gt() command and specify the desired row.

在執行基本的tidyverse動詞以按所需形狀過濾和分組資料後,我們呼叫gt()命令並指定所需行。

Image for post
Image for post

Add Column Spanners

新增列扳手

As evident, the above data is the simplest and rawest data table created. We now proceed to format it. We begin by adding column spanners and bringing male and female columns in their respective hood and customise their font and alignment.

顯而易見,以上資料是建立的最簡單和最原始的資料表。 現在,我們進行格式化。 我們首先新增列扳手,並將公母列帶入各自的罩中,然後自定義其字型和對齊方式。

Image for post
Image for post

Create Row Summary Statistics & Format Data

建立行摘要統計資訊和格式資料

We can also add a descriptive statistic specifying mean or totals per metric within the table. This could enable relative comparison and could lead to rich insights. Here, I have calculated the yearly average bill length, bill depth and flipper in each island and formatted the number to include no decimals.

我們還可以在表中新增描述性統計資訊,以指定每個指標的平均值或總計。 這樣可以進行相對比較,並可以得出豐富的見解。 在這裡,我計算了每個島嶼的年平均賬單長度,賬單深度和腳蹼,並對數字進行了格式化,使其不包含小數。

Image for post
Image for post

Add Colour & Footnote

新增顏色和腳註

Now the fun part, I have used one of the plethora of palettes available via the palatteer package and conditionally formatted shade of the table in accordance to the numeric value of the cell it represents. The gt package also a feature to add a footnote to the table. I have used it here to specify the metric in which the bill length, bill depth and flipper length and body mass is measured.

現在最有趣的部分是,我使用了可通過palatteer軟體包獲得的大量調色盤之一,並根據表所表示的單元格的數值對錶進行了條件格式化。 gt包還具有在表中新增腳註的功能。 我在這裡使用它來指定度量標準,在該度量標準中可以測量鈔票長度,鈔票深度以及腳蹼長度和體重。

Image for post
Image for post

Add Table Title, Subtitle & Change Column Labels

新增表格標題,字幕和更改列標籤

While the colours have bought in buoyancy to the table, a title is much needed. The package also enables the use of markdown text in the title/subtitle. Here I have used the emo::ji package to add cute little penguin emoji’s in my title. Since the column labels were still messy, I have also renamed & formatted column tables to a shorter, more readable title.

儘管桌子上的顏色使它們浮起來,但仍然很需要標題。 該包還允許在標題/副標題中使用降價文字。 在這裡,我使用emo::ji包在標題中添加了可愛的小企鵝表情符號。 由於列標籤仍然雜亂無章,因此我還對列表進行了重新命名和格式化,以使其標題更短,更易讀。

Image for post
Image for post

Add Background Colour & Format Borders

新增背景顏色和格式邊框

I know proceed to add custom colours to the remaining whitespace, i.e title, subtitle, row average and footnote. I have specified the desired HEX code referring to this colour brewer for aesthetic advice . I have also added dotted borders to the year rows and have thickened the borders along the island rows to amplify differentiation and boost readability.

我知道繼續為剩餘的空白新增自定義顏色,即標題,副標題,行平均值和腳註。 我已經指定了與色料調製器相關的所需十六進位制程式碼,以提供美學建議。 我還為年份行添加了虛線邊框,並增加了沿島行的邊框,以擴大差異性並提高可讀性。

Image for post
Image for post

Adjust Font , Alignment & Add Source Note

調整字型,對齊方式並新增源註釋

As a last finishing touch and i order to ensure font uniformity, I have applied the Oswald font across the table and have centre aligned each cell. Lastly, below the footnote, I have added a source-note that states the data source.

最後,為了確保字型均勻性,我在表格上應用了Oswald字型,並使每個單元格居中對齊。 最後,在腳註下方,我添加了一個註明資料來源的來源說明。

Image for post
Image for post

And that’s the final masterpiece! :D

這就是最終的傑作! :D

Hope you enjoyed reading. You can reach out to me here & here!

希望您喜歡閱讀。 您可以在這裡這裡與我聯絡!

翻譯自: https://towardsdatascience.com/exploring-the-gt-grammar-of-tables-package-in-r-7fff9d0b40cd

千牛包表包下載