I read multiple posts showing how to use grid.table in order to create an image off a dataframe. I'm coding the below function that perhaps could be of help to other people too. The two questions I can't figure out are:
- The background color of the column headers comes out as gray. How can I customize it to say yellow?
- How can I change the font size of just column headers?
func_table_image <- function(df, col_header_color='navyblue', col_header_fontface=4,
show_rows_header=F,
use_sample_df=F){
if(use_sample_df){
df <- data.frame(TICKER=c("IBM", "APPL", "ZM", "NKE", "GS"),
SECTOR=c('Technology',"Technology","Software","Consumer","Financials"),
PX_CHG=c(4.2, 10, 29.5, 9, 31), stringsAsFactors = F)
}
t1 <- ttheme_default(
# Fg_params is used to modify any color or size issue to do with font
colhead=list(fg_params=list(col=col_header_color, fontface = col_header_fontface)),
core=list(
#fg_params=list(fontface=c(rep("plain", nrow(df))))
fg_params=list(fontface=3))
# bg_params is used to modify structures and layouts of boxes.
, bg_params = list(fill=c(rep(c("#DDE6F7", "#FFECDB"),
length.out=nrow(df))
)
#,alpha = rep(c(1,0.5), each=5),
, col=NA
)
)
)
if(show_rows_header){
grid.table(df, theme = t1)
}else{
grid.table(df, theme = t1, rows = NULL)
}
}
func_table_image(df, col_header_color='navyblue', col_header_fontface=4,
use_sample_df=T)