How to adjust the Font Size of tableGrob
You can do this via themes:
mytheme <- gridExtra::ttheme_default(
core = list(fg_params=list(cex = 2.0)),
colhead = list(fg_params=list(cex = 1.0)),
rowhead = list(fg_params=list(cex = 1.0)))
myt <- gridExtra::tableGrob(mtcars[1:5, 1:5], theme = mytheme)
grid.draw(myt)
There are a number of other examples in browseVignettes("gridExtra")
-- look at the tableGrob
examples. A great deal of control is possible.
tableGrob: resizing a table (changing font size) drawn on top of ggplot using annotation_custom
If you just want all the text to be smaller in your table, use base_size
in ttheme_default
:
library(ggplot2)
library(grid)
library(gridExtra)
df <- data.frame(x=seq(1,10),y=seq(11,20))
table <- data.frame(x=seq(1,3),y=seq(4,6))
ggplot(df,aes(x=x,y=y)) +
geom_point() +
annotation_custom(tableGrob(table,rows=NULL, theme = ttheme_default(base_size = 8)),
xmin=0,xmax=3,ymin=15,ymax=20)
Created on 2020-03-05 by the reprex package (v0.3.0)
How do I fit a very wide grid.table or tableGrob to fit on a pdf page?
I got this done using font sizes. Not the best solution (requires manual intervention) but maybe someone can contribute something more elegant.
termTable = tableGrob(terms, h.even.alpha=1, h.odd.alpha=1, v.even.alpha=0.5, v.odd.alpha=1, core.just='left', rows=c(),
gpar.coretext =gpar(fontsize=8),
gpar.coltext=gpar(fontsize=10, fontface='bold'),
gpar.rowtext=gpar(fontsize=10, fontface='bold')
)
Increase tableGrob column title size and cell size
You can do this by changing the padding
of the colhead
within the theme
.
eg
thm <- ttheme_default(colhead =
# first unit is the wdith, and second the height
list(padding=unit.c(unit(4, "mm"), unit(10, "mm"))))
tableGrob(d, theme=thm)
To get an idea of what you can change you can view ttheme_default()
in the terminal
Change font size of all existing grobs in table
You could do it like this:
lapply(1:length(g$grobs),function(i){g$grobs[[i]]$gp$fontsize <<- 35})
grid.draw(g)
tableGrob formatting
Here are some ideas that could be useful for solving your problem.
library(grid)
library(gridExtra)
library(gtable)
# Build table
tabla_fin <- rbind(
c("","2015\nabril","2016\nabril","2017\nmarzo","2017\nabril"),
c("Pedidos",-23.3,-13.8,-26,-39),
c("Existencias",7.6,1.2,5.3,10.5),
c("Expectativas",30.7,32.7,28.7,24),
c("ICI",-0.1,5.9,-0.9,-8.5),
c("ICI Desest*",0.4,5.7,-3.2,-6.9))
colnames(tabla_fin) <- tabla_fin[1,]
tabla_fin <- tabla_fin[-1,]
rownames(tabla_fin) <- tabla_fin[,1]
tabla_fin <- tabla_fin[,-1]
print(tabla_fin)
# 2015\nabril 2016\nabril 2017\nmarzo 2017\nabril
# Pedidos "-23.3" "-13.8" "-26" "-39"
# Existencias "7.6" "1.2" "5.3" "10.5"
# Expectativas "30.7" "32.7" "28.7" "24"
# ICI "-0.1" "5.9" "-0.9" "-8.5"
# ICI Desest* "0.4" "5.7" "-3.2" "-6.9"
# Install Calibri font using the extrafont package
# read the help of font_install()
library(extrafont)
loadfonts(device="win")
# font_install()
# Theme for text tables
t1 <- ttheme_default(
core=list(
fg_params=list(fontface=rep("plain", 6), fontfamily="Calibri",
x=1, hjust=1),
bg_params = list(
fill=c(rep("white",3),"grey80","grey90"),
col=c("white","white","white","grey80","grey90"),
alpha = rep(1,5))
),
rowhead=list(
fg_params=list(x=0, hjust=0, fontface="plain", fontfamily="Calibri"),
bg_params = list(
fill=c("white",rep("white",3),"grey80","grey90"),
col=c("white","white","white","white","grey80","grey90"),
alpha = rep(1,5))
),
colhead=list(
fg_params=list(fontface="plain",fontfamily="Calibri"),
bg_params = list(
fill="grey70",
col="grey70",
alpha = rep(1,5))
)
)
# Create gtable containing text grobs
t <- tableGrob(tabla_fin, theme=t1)
# Add horizontal and vertical lines
t <- gtable_add_grob(t,
grobs = segmentsGrob( # line across the bottom
x0 = unit(0,"npc"),
y0 = unit(0,"npc"),
x1 = unit(1,"npc"),
y1 = unit(0,"npc"),
gp = gpar(lwd = 4.0)),
t = 1, b = 1, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
grobs = segmentsGrob( # line across the bottom
x0 = unit(0,"npc"),
y0 = unit(0,"npc"),
x1 = unit(1,"npc"),
y1 = unit(0,"npc"),
gp = gpar(lwd = 4.0)),
t = 4, b = 4, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
grobs = segmentsGrob( # line across the bottom
x0 = unit(0,"npc"),
y0 = unit(0,"npc"),
x1 = unit(0,"npc"),
y1 = unit(1,"npc"),
gp = gpar(lwd = 4.0)),
t = 1, b = nrow(t), l = 4, r =4)
# Draw table
grid.newpage()
grid.draw(t)
Here is the table:
tableGrob: set the height and width of a grid.table
The solution I went with was to manipulate the length of the levels like this:
longest <- 0
longestnumber <- 0
for (i in 1:length(levels(x))){
if (longest < nchar(levels(x))[i]){
longest <- nchar(levels(x))[i]
longestnumber <- i
}
}
for (i in 1:(100-longest)){
levels(x)[longestnumber] <- paste(levels(x)[longestnumber], " ", sep="")
}
That way I can control the width of the table
Related Topics
Simple R 3D Interpolation/Surface Plot
How to Filter on Partial Match Using Sparklyr
Transpose Only Certain Columns in Data.Frame
R Corpus Is Messing Up My Utf-8 Encoded Text
Suppress Automatic Output to Console in R
R - Delete Consecutive (Only) Duplicates
How to Plot a Combined Bar and Line Plot in Ggplot2
What's the Easiest Way to Deploy an API Incorporating R Functions
How to Access the Name of the Variable Assigned to the Result of a Function Within the Function
R 3.5 Is Not Available for Linux
Specify Position of Geom_Text by Keywords Like "Top", "Bottom", "Left", "Right", "Center"
Gcc: Error: Libgomp.Spec: No Such File or Directory with Amazon Linux 2017.09.1
Replace Missing Values with a Value from Another Column
Setting Column Width in R Shiny Datatable Does Not Work in Case of Lots of Column
Avoid Ggplot2 to Partially Cut Axis Text
How to Neatly Align the Regression Equation and R2 and P Value