0

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:

  1. The background color of the column headers comes out as gray. How can I customize it to say yellow?
  2. 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)
user20650
  • 24,654
  • 5
  • 56
  • 91
Angelo
  • 1,594
  • 5
  • 17
  • 50
  • have a look at the results of `ttheme_default()` in the terminal. You can change `bg_params` and see the `fg_params` for the `fontsize` – user20650 Jul 01 '21 at 22:10
  • I thin you also have a misplaced / extra parenthesis after `fg_params=list(fontface=3))` in the `core` list – user20650 Jul 01 '21 at 22:18
  • is there any chance you could show me how to use fontsize in the function above please? I'm really confused with the way I should use this library and it may help other people too. Thank you – Angelo Jul 01 '21 at 22:47
  • `grid.table(df, theme=ttheme_default( colhead =list(fg_params=list(col="yellow", fontface = 4L, fontsize=30), bg_params=list(fill="red"))))`. You could also use `cex` (instead of fontsize) as shown in the answer of one of the duplicate questions. – user20650 Jul 01 '21 at 23:06

0 Answers0