- 3 Minutes Wednesdays
- Posts
- 3MW (Images, Icons and ggplots in tables)
3MW (Images, Icons and ggplots in tables)
Guten Tag!
Many greetings from Ulm, Germany. Let me start with one short announcement.
Next week, I'm taking over the @Iamscicomm channel on Twitter for one week. I'm pretty excited about this and worked out a neat schedule.
Coming back to this week's newsletter, let us continue to talk about tables with {gt} and {gtExtras}.
Include icons in your tables
Adding icons to any {gt} table is easy. You don't actually need anything but the icon itself. Thankfully, R has just the right {emoji} package to get the work done.
Once you've got the data, just send that to gt() and you're done. Of course, you can then tweak the table like you normally would (e.g. add titles and a footnote).
The same works with fontawesome icons as well. But you have to be a little bit more careful in that case. The {fontawesome} package will give you an icon as HTML code. Thus, you need to wrap the output from fontawesome::fa() in html() (Code).
Include images in your tables
The easiest way to add images to your table is to rely on gt_img_rows() from {gtExtras}. Just add a column with file paths or URLs of images to your table. Then, target that column with gt_img_rows() and your work is done.
For example, you could use this to get images of the last three British prime ministers from Wikipedia and use them in a table (Code):
Alternatively, you can use gtExtras::gt_img_circle(). This will give you round images (Code):
Add arbitrary plots to your table
Last week, we added spark lines and bullet charts to our tables. But we don't have to stop there. We can add any ggplot. For example, we could look at penguins' body weight and use a violin plot to visualize the weight's distribution.
At first, it's a bit tricky to make that work. So, let me untangle what you need to do:
Write a function that creates the ggplot you want to include. This function will depend on some arguments. In our case, that argument is a species.
Include a new column in your table containing the arguments for the function you've just created. At that stage, our column Distribution contains only the arguments: Adelie, Chinstrap and Gentoo.
Now comes the hard part. Send your {gt} table to gt::text_transform(). In conjunction with your own plotting function, it will transform the content of the Distribution column to actual ggplots.
Notice that I've had to use an additional function inside of text_transform(). Really that function is just a wrapper around our plotting function and gt::ggplot_image(). The latter function helps to format the plot for our table.
To get a better feeling for what's happening, you can play around with the full code here.
Recommended Resources
You like R but you also like Python? Then check out this blog post from Karina Bartolomé. It is truly a bridge between Python and R. Not only does Karina create superb tables with {gt}, she also replicates the same tables with Python as well.
That's a wrap! As always, I'm happy to hear your feedback about this week's issue. Feel free to write me on Twitter or reply to this mail.
Enjoy the rest of your day!Albert
Did you like this post? Whenever you feel like it, you can support me with a coffee or a membership.
Reply