I've been trying to plot tables in R (see: Plot a table where specific rows can be highlighted (gridExtra alternative)) and ggplot2
is very close to being able to do it in a sensible manner. The main feature that stops it being easy is that you cannot move the x-axis, so you can't use it as a header.
Instead I have managed the following hack:
Libraries
library(ggplot2)
library(data.table)
Example data:
DT <- data.table(Meal = c("Spam", "Eggs", "Spam\nand Eggs"), Price = c(5.00,4.50, 7.00), Calories = c(200,250,400))
DT <- melt(DT, id = "Meal")
DT <- DT[, highlightCell := (value == min(value)), by = variable]
DT
Meal value variable highlightCell
1: Spam 5.0 Price FALSE
2: Eggs 4.5 Price TRUE
3: Spam\nand Eggs 7.0 Price FALSE
4: Spam 200.0 Calories TRUE
5: Eggs 250.0 Calories FALSE
6: Spam\nand Eggs 400.0 Calories FALSE
Table making code:
table_theme <- theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
cellfontsize <- 12
headerfontsize <- 7
headerColour <- "grey"
ggplot(DT, aes(x = variable, y=Meal, label = value, fill = highlightCell, xmin = as.numeric(factor(variable)) - 0.5, xmax = as.numeric(factor(variable)) + 0.5, ymin = as.numeric(factor(Meal)) - 0.5, ymax = as.numeric(factor(Meal)) + 0.5)) +
# This does the cells
geom_rect() + geom_text(size = cellfontsize) +
# Next four lines do the headers (ugly)
geom_rect(aes(ymin = length(levels(factor(Meal))) + 0.5, ymax = length(levels(factor(Meal))) + 1.5), fill = headerColour) +
geom_rect(aes(xmin = -0.5, xmax=0.5), fill = headerColour) +
geom_text(aes(y = length(levels(factor(Meal))) + 1, label = variable), size = headerfontsize) +
geom_text(aes(x = 0, label = Meal), size = headerfontsize) +
table_theme + coord_cartesian(xlim=c(-1,3), ylim=c(-1,5))
Ideally:
Ideally to make this look like ggplot, I'd like to wrap up those above lines so that they just pick up on the aesthetics in the ggplot command, but I have no idea how to "access" that information to create a new geom_
type function.
For example, the ideal syntax would probably be something like:
ggplot(DT, aes(x=variable, y=Meal, label=value, fill=highlightCell)) +
geom_tablecells() +
geom_tablerowheader() + geom_tablecolheader() + theme_table()
Can anyone give any pointers how one wrap the above into new geom functions?
I tried, this but it doesn't work:
geom_tablecells <- function(...) {
return(geom_test(...) + geom_rect(..., size = cellfontsize))
}