4 Exporting Results
The goal of this section is to learn how to take the figures/tables we created in the tidyverse section and export them directly into a form we can read into LaTex.
Specifically:
- Xtable
- ggsave
4.1 Saving Plots
See ggplot section in the tidyverse section previously to learn how to generate a plot. Once we have generated a plot we like, let’s save it.
To do this we will use the ggsave command.
This will take the last graphic in memory and save it in the location we specify. We can also tell ggplot to make the plot specific dimensions which I highly recommend doing. If you do not specify dimensions, they can be dependent on the size of your viewer in RStudio.
4.2 Tables to LaTex
Let’s learn how to export our summary table we created in the dplyr section. Just as a review here is how we created it.
indiana_laborforce<- # 1 # 1
unemp_data %>% # 2 # 2
filter(State_fips=="18") %>% # 3 # 3
group_by(Year) %>% # 4 # 4
summarise(Min=min(Labor_force), # 5 # 5
p10th=quantile(Labor_force, c(0.1)), # 6 # 6
p25th=quantile(Labor_force, c(0.25)), # 7 # 7
p50th=quantile(Labor_force, c(0.5)), # 8 # 8
p75th=quantile(Labor_force, c(0.75)), # 9 # 9
p90th=quantile(Labor_force, c(0.9)), # 10 # 10
Max=max(Labor_force), # 11 # 11
Average=mean(Labor_force), # 12 # 12
StDev=sd(Labor_force)) # 13 # 13
Now let’s load in the package xtable. This package allows us to print data.frames into a tex table environment. Let’s first inspect the output for just running the base function with minimal options.
xtable(indiana_laborforce) # 1 # 1
## % latex table generated in R 4.1.1 by xtable 1.8-4 package
## % Sat Jan 01 17:21:37 2022
## \begin{table}[ht]
## \centering
## \begin{tabular}{rlrrrrrrrrr}
## \hline
## & Year & Min & p10th & p25th & p50th & p75th & p90th & Max & Average & StDev \\
## \hline
## 1 & 1990 & 2629.00 & 6639.30 & 9617.50 & 14858.50 & 30089.50 & 60784.50 & 424053.00 & 30593.65 & 52989.19 \\
## 2 & 1991 & 2541.00 & 6380.50 & 9480.00 & 14713.00 & 30131.00 & 62309.90 & 421789.00 & 30357.00 & 52670.08 \\
## 3 & 1992 & 2550.00 & 6284.20 & 9615.50 & 15169.50 & 31117.00 & 63623.20 & 429239.00 & 31058.60 & 53551.02 \\
## 4 & 1993 & 2607.00 & 6200.60 & 10037.75 & 15652.50 & 32034.75 & 64549.90 & 438377.00 & 32046.84 & 54683.97 \\
## 5 & 1994 & 2681.00 & 6453.10 & 10480.50 & 16472.00 & 33808.25 & 66506.30 & 453457.00 & 33308.01 & 56427.35 \\
## 6 & 1995 & 2714.00 & 6543.20 & 10994.75 & 16648.50 & 34588.00 & 67364.50 & 459603.00 & 34078.00 & 57219.27 \\
## 7 & 1996 & 2724.00 & 6522.60 & 10827.50 & 16551.00 & 34597.25 & 66487.50 & 453728.00 & 33800.80 & 56670.37 \\
## 8 & 1997 & 2698.00 & 6585.30 & 10966.00 & 16671.50 & 34786.50 & 66195.30 & 453928.00 & 33919.72 & 56870.28 \\
## 9 & 1998 & 2701.00 & 6629.70 & 11008.75 & 16660.00 & 34150.50 & 65887.60 & 452412.00 & 33947.47 & 56827.82 \\
## 10 & 1999 & 2691.00 & 6496.70 & 10986.75 & 16958.50 & 33968.50 & 65196.00 & 448732.00 & 33965.91 & 56434.89 \\
## 11 & 2000 & 2985.00 & 7053.50 & 10816.50 & 16984.50 & 34996.50 & 63718.60 & 455135.00 & 33982.50 & 56818.02 \\
## 12 & 2001 & 2982.00 & 7085.70 & 10709.00 & 17127.50 & 34674.75 & 64832.00 & 459505.00 & 34140.32 & 57318.59 \\
## 13 & 2002 & 3051.00 & 7040.70 & 10802.75 & 17244.50 & 34974.75 & 66572.30 & 462330.00 & 34469.34 & 57807.67 \\
## 14 & 2003 & 3092.00 & 6941.00 & 10709.50 & 17199.00 & 34514.75 & 67751.50 & 464331.00 & 34597.76 & 58056.22 \\
## 15 & 2004 & 3109.00 & 6815.20 & 10547.00 & 17083.50 & 33431.50 & 68476.20 & 459140.00 & 34432.72 & 57583.09 \\
## 16 & 2005 & 3152.00 & 7075.80 & 10702.75 & 17357.50 & 33606.00 & 69985.60 & 459492.00 & 34841.75 & 57925.42 \\
## 17 & 2006 & 3107.00 & 6883.10 & 10620.50 & 17326.50 & 34554.50 & 72792.00 & 461468.00 & 35164.35 & 58484.53 \\
## 18 & 2007 & 3026.00 & 6816.70 & 10447.50 & 16881.00 & 34304.50 & 73128.10 & 460046.00 & 34866.21 & 58342.88 \\
## 19 & 2008 & 3044.00 & 6816.20 & 10488.00 & 16673.50 & 34065.50 & 75072.00 & 463200.00 & 35131.49 & 58819.13 \\
## 20 & 2009 & 3088.00 & 6758.40 & 10434.25 & 16452.00 & 33700.25 & 74434.10 & 457524.00 & 34717.45 & 58083.80 \\
## 21 & 2010 & 3263.00 & 6440.50 & 10308.00 & 16622.00 & 33921.75 & 74406.00 & 452836.00 & 34512.97 & 57806.16 \\
## 22 & 2011 & 3210.00 & 6459.90 & 10184.25 & 16630.50 & 33804.00 & 75620.50 & 456011.00 & 34586.91 & 58199.53 \\
## 23 & 2012 & 3181.00 & 6336.20 & 10058.50 & 16453.00 & 32924.00 & 76458.00 & 458377.00 & 34454.88 & 58388.80 \\
## 24 & 2013 & 3165.00 & 6325.90 & 9900.50 & 16289.50 & 33053.00 & 78149.50 & 463346.00 & 34656.59 & 58983.65 \\
## 25 & 2014 & 3216.00 & 6401.20 & 9851.50 & 16524.00 & 33215.50 & 80113.30 & 467169.00 & 35052.00 & 59579.47 \\
## 26 & 2015 & 3178.00 & 6341.80 & 9924.75 & 16908.00 & 33960.00 & 82327.70 & 472330.00 & 35508.30 & 60275.44 \\
## 27 & 2016 & 3226.00 & 6444.80 & 9930.50 & 17093.50 & 35149.00 & 85223.00 & 481434.00 & 36178.11 & 61517.07 \\
## 28 & 2017 & 3153.00 & 6512.50 & 9783.75 & 17119.00 & 35516.50 & 85487.60 & 483607.00 & 36243.96 & 61840.36 \\
## 29 & 2018 & 3189.00 & 6561.90 & 9943.50 & 17334.50 & 36211.00 & 86905.80 & 488698.00 & 36756.59 & 62659.34 \\
## 30 & 2019 & 3201.00 & 6615.60 & 9812.75 & 17243.00 & 36470.50 & 87146.90 & 492967.00 & 36819.25 & 63114.77 \\
## \hline
## \end{tabular}
## \end{table}
We could then copy and paste this output into latex. However, we can tell xtable and R to save a tex file with this in it. Notice the pathing used. This is a latex table so we are keeping ourselves organized by 1) giving the tex file a useful name and 2) saving it within our tables folder. This may seem extra tedious now but come generating hundreds of plots and tables, organazation can be key to keeping everything straight.
print.xtable( # 1 # 1
xtable(indiana_laborforce), # 2 # 2
file=paste0(workingdir, "tables/indiana_laborforce.tex")) # 3 # 3
Try building this table in a pdf through LaTex
Let’s now customize this table more. First, let’s get rid of row names as they are not meaningful for us here. Also, if we want to later fix this table in a certain place in a tex document we want a table placement H.
print.xtable( # 1 # 1
xtable(indiana_laborforce), # 2 # 2
table.placement="H", # 3 # 3
include.rownames=FALSE, # 4 # 4
file=paste0(workingdir, "tables/indiana_laborforce.tex"), # 5 # 5
) # 6 # 6
Let’s add a bit more customization to this table.
print.xtable( # 1 # 1
xtable( # 2 # 2
indiana_laborforce, # 3 # 3
digits=0, # 4 # 4
label= "tab:indiana_laborforce", # 5 # 5
align = c("|c|","|c|","c","c","c","c","c","c","c","c","c|")), # 6 # 6
table.placement="H", # 7 # 7
include.rownames=FALSE, # 8 # 8
file=paste0(folder_tables, "/indiana_laborforce.tex")) # 9 # 9
- Line #4: Round all digits to the nearest integer.
- Line #5: add a label to the table which you can then reference to in your main tex document.
- Line #6: if we want to have a custom alignment for our table. (Note: this command requires 1 more column of arguments than there is columns in your table. This is due to it printing the row names. However if you later define include.rownames as FALSE as I do here. It will get rid of the first column of row names.)
There are many other packages that can assist in creating tex tables. We will later use stargazer for reporting regression results. I like using xtable due to it’s ease to customize. Please leave me a note below if you have any favorite packages for creating tex tables.