I am trying to format the table using gridExtra package. The gridExtra package I have is 2.0 and R version is 3.2.1
I was going through answers here on stackoverflow about the formatting and the suggested options seem to work only with older version of the package. For example,
grid.table(data, h.even.alpha = 1, h.odd.alpha = 0,
v.even.alpha = 1, v.odd.alpha = 1,
gpar.corefill, gpar.coretext)
All of these options are shown as "unused arguments" in the latest version.
Searching further, I found that in new gridExtra package, formatting is defined probably inside theme, example -
tt <- ttheme_default(core=list(fg_params=list(hjust=1, x=0.95)),
colhead=list(fg_params=list(col="brown"))
and then doing
grid.table(data, theme=tt).
What I could not found was how these options inside theme is defined and how all the formatting which was possible in older version can now be done.
In particular, I am looking to do -
Left justification of columns
commas for big.marks (10000 as 10,000)
different row colors for even and odd row numbers
column header color
not showing row names (something like row.names=FALSE)
This recent answer shows how to alter the parameters, and Baptiste gives a link to further examples. As you notice in your question, to alter the formatting you use the theme argument; you can see what parameters to alter by looking at the output of ttheme_default()
# New theme paramters
myt <- ttheme_default(
# Use hjust and x to left justify the text
# Alternate the row fill colours
core = list(fg_params=list(hjust = 1, x=1),
bg_params=list(fill=c("yellow", "pink"))),
# Change column header to white text and red background
colhead = list(fg_params=list(col="white"),
bg_params=list(fill="red"))
)
# Example data - create some large numbers
dat <- mtcars[1:5,1:5]
dat$mpg <- dat$mpg*1000
grid.newpage()
grid.draw(tableGrob(format(dat, big.mark=","), theme=myt, rows=NULL))
The big.mark argument of format is used to add the comma separator, and rownames are removed using the rows=NULL argument.
Related
Using flextable to make a table, I would like a column heading to be pi (with a hat) and an i subscript. With flextable 6 I see that I can add basic formatting to titles using the compose() function. I can get pi sub i easily but I don't see how to put the hat over the pi.
test <- data.frame(hat_pi_i= c(0.1, 0.9))
# devtools::install_github("davidgohel/flextable") # version 6.x
# devtools::install_github("davidgohel/officer") # for flextable 6 to work
library(flextable)
# compose conflicts with purrr & igraph
flextable(test) %>%
compose(part = "header", j = "hat_pi_i",
value = as_paragraph("hat π", as_sub("i")))
Is it possible to tweak the title above to put the hat on the pi instead of as a word next to it?
There's a hackish way to do it:
flextable(test) %>%
compose(part = "header", j = "hat_pi_i",
value = as_paragraph("\U1D70B\U0302", as_sub("i")))
This uses Unicode characters: \U1D70B is the math style pi, and \U0302 says to put a circumflex on the previous character. You could also do the first char with π, but it doesn't look as good, because (at least on my system) the circumflex ends up misplaced:
I'm trying to create a table in R with formattable. I can have already used formattable to create a table where the color depended on the values. But there is one cell that I just want to be red, no matter what is in the cell, and I can't figure out how to do this.
result_table <- cbind(Normal = c(1,2,3), Fraud = c(4,5,6))
row.names(result_table) <- c('Normal', "Suspicious", "Fraud")
my_df <- as.data.frame(result_table)
formattable(my_df)
I want to color normal vs normal green, fraud vs fraud green, normal vs fraud red, fraud vs normal red. But since I'm using formattable also for the other tables in my report, I'd like to use it here as well (so that all tables in my report have the same style.)
OK, so here is a solution, although not a perfect one. First, we define the formatters – functions that take the data and turn it into HTML code:
red.f <- formatter("span", style=x~style(color="red"))
green.f <- formatter("span", style=x~style(color="green"))
It is possible to make these functions conditional, but unfortunately neither is formatter well documented nor the authors foresaw your problem.
Now then.
my_df2 <- sapply(colnames(my_df), function(cn) {
sprintf(
ifelse(cn == rownames(my_df), green.f("%s"), red.f("%s")),
my_df[,cn]
)
})
my_df2 <- data.frame(my_df2)
rownames(my_df2) <- rownames(my_df)
formattable(my_df2)
Explanation: I cannot find a way to put the ifelse inside of the formatter. Therefore, I create a character vector with %s as a placeholder and fill it out with values from the column using sprintf. Then, I combine the vectors to a matrix using sapply, turn it into a data frame, add row names and presto.
I'm looking for a way to print out a table from R, but with formatting within a cell, like having some of the text within a cell be bold or italic. Normally I would first make the appropriate data.frame/tibble and then I'd format and print it using a package like huxtable or kable. Looking over documentation for huxtable or kableExtra, it seems as though both packages treat formatting as properties of cells, implying that within-cell formatting is either unsupported or must be implemented some other way.
If I was making a ggplot, I'd use expression for text formatting, e.g.
library(tidyverse)
ggplot(data=mtcars) +
ggtitle(expression(paste(bold("bold part"), " not bold part")))
I thought I could be clever by putting expressions into a data.frame, but this doesn't seem to be supported:
data.frame(var = c(expression(paste(bold("bold part"), "not bold part")),
expression(paste(bold("bold part"), "not bold part"))
))
#> Error in as.data.frame.default(x[[i]], optional = TRUE): cannot coerce class ""expression"" to a data.frame
If you want to make changes to data tables, I recommend you use the grid and gridExtra packages to construct your table and then make changes to the theme parameters.
Without any data to play with I can't see exactly what you want but here's a general idea of what you could do (see below). I've included other aesthetic parameters, for future reference.
You could then generate a pdf output to your C drive, which could then be printed.
d <- data.frame(A = c(1,2,3,4,5),
B = c(6,7,8,9,10),
C = c(11,12,13,14,15))
pdf("Test.pdf", height = 11, width = 10)
grid.table(d, rows = NULL, theme = ttheme_minimal(
core=list(fg_params=list(
hjust=0,
x=0.1,
fontface=matrix(c(1,2,3))))))
dev.off()
Re huxtable, you're correct, but you can get round it. Here's a 1 row, 1 column example, assuming you are printing to HTML:
my_hux <- huxtable("<b>Bold part</b> Not bold part")
escape_contents(my_hux)[1, 1] <- FALSE
You can include arbitrary HTML. Something similar would work for TeX, obviously with TeX formatting instead.
I am trying to print an apostrophe for the column name below in a table using tableGrob
"Kendall's~tau"
The end result is that the whole label is italicized without the ~ and tau being interpreted:
How do I correctly specify this?
I don't think it's helpful, but this is the theme that I've specified to tableGrob:
table_theme <- ttheme_default(
core = list(fg_params=list(fontsize = 6)),
colhead = list(fg_params=list(fontsize = 6, parse=TRUE)),
rowhead = list(fg_params=list(fontsize = 6, parse=TRUE)),
padding = unit(c(2, 3), "mm"))
The column name is interpreted via plotmath in grDevices -- the standard way of specifying mathematical annotation in figures generated in R.
Again it has nothing to do with how to specify the expression itself, but here is the table constructor:
tableGrob(stats_df,
theme = table_theme,
rows = c("Kendall's~tau"))
Here's a reproducible example:
library(gridExtra)
library(grid)
data(iris)
table_theme <- ttheme_default(rowhead = list(fg_params=list(parse=TRUE)))
grid.table(head(iris),
rows = c(letters[c(1:4)], "plotmath~works~omega", "Kendall's~tau"),
theme = table_theme)
This works:
library(gridExtra)
library(grid)
data(iris)
table_theme <- ttheme_default(rowhead = list(fg_params=list(parse=TRUE)))
grid.table(head(iris),
rows = c(letters[c(1:4)], "plotmath~works~omega", "Kendall's"~tau),
theme = table_theme)
Try with a backslash in your expression like "Kendall\'s~tau". It should work then.
I tried to use apostrophe with expressions to plot in ggplot. In my database is invalid to use ' in expressions, but this worked
expression(paste(u,"'",(t),sep=""))
But this "paste" also cause bad behaviour of the subindex expression expression(U[0]). So to use both together, this one worked
paste(expression(paste("u","'",sep="")),"/U[0]",sep="")
If anyone knows a easier way, I'd be very glad.
If your apostrophe is embedded in a longer string, along with special symbols like ~, these solutions will not work. The only thing I've found to work is using regular expression substitution.
#This doesn't work
stringWithApostrophe="Matt's and Louise's diner~...and~also Ben's diner~X^2"
qplot(1:10,1:10)+annotate("text",x=2,y=4,label=stringWithApostrophe,parse=T)
Error: "Error in parse(text = text[[i]]) : :1:5: unexpected string constant"
The problem is the special characters like tilde and apostrophe happening in the same quoted segment. So you have to separate "Matt's" from "Louise's" and "~". Here's the code to do that.
stringWithApostrophe2<-stringr::str_replace_all(pattern = "([^~()*]*'[^~()*']*)",replacement = "\"\\1\"",string=stringWithApostrophe)
qplot(1:10,1:10)+annotate("text",x=2,y=8,hjust=0,label=stringWithApostrophe2,parse=T)
Plots successfully. The final expression that plotmath in R parses correctly is:
""Matt's and Louise's diner"~...and~"also Ben's diner"~X^2"
Is there a way to tell tableGrob via themes to change the color and format (make bold) of a specific line?
I get the following output using tableGrob:
I however would like to reach the following aesthetic and make specific lines bold and change their color like this example:
Based on the following example: enter link description here
t1 <- ttheme_default(core=list(
fg_params=list(fontface=c(rep("plain", 4), "bold.italic")),
bg_params = list(fill=c(rep(c("grey95", "grey90"),
length.out=4), "#6BAED6"),
alpha = rep(c(1,0.5), each=5))
))
grid.table(iris[1:5, 1:3], theme = t1)
The color of the row is changed every 4 lines. which results to this:
result
However, I need to make it specific based on condition.
If you poke at the link in my comment for how to "find" grobs, then you can do things like:
library(grid)
library(gridExtra)
tg <- tableGrob(iris[1:5, 1:3])
for (i in c(19,24,29)) tg$grobs[[i]] <- editGrob(tg$grobs[[i]], gp=gpar(col="white"))
for (i in c(34,39,44)) tg$grobs[[i]] <- editGrob(tg$grobs[[i]], gp=gpar(fill="blue"))
grid.newpage()
grid.draw(tg)
If this is regular, parameterized report, then it's a "once-and-done" operation to figure out the cell positions and apply your custom formatting.
Otherwise, definitely develop a set of styling functions for targeting "cells" like you would in google sheets or Excel.