user3055869
user3055869

Reputation: 45

Changing font family of geom_text located inside a package

Using a R package called Likert which uses ggplot2 and I want to change the font family. In the package it looks like this:

    if(plot.percent.high) {
        p <- p + geom_text(data=lsum, y=100, aes(x=Item,
                        label=paste0(round(high), '%')), 
                        size=text.size, hjust=-.2, color=text.color)
    }

Was wondering how to change the geom_text from outside the package without knowing a lot of this information. For labels you can just use theme, but theme doesn't seem to work with this.

Currently

p = plot(lik) + theme(text = element_text(family = "Georgia"))

to change everything else to Georgia.

Upvotes: 2

Views: 1413

Answers (1)

user20650
user20650

Reputation: 25854

Re comment, you can edit the grobs to change the fontfamily in the geom_text calls.

Code is wrapped in a function as you are wanting to replicate the graphs.

library(likert)

# example
data(pisaitems)
items28 <- pisaitems[, substr(names(pisaitems), 1, 5) == "ST24Q"]
l28 <- likert(items28)

# helper function - takes likert plot as input
# loops through the geom_text calls editing the font family
grid_fam <- function(p, fam="Georgia") 
                  {
                  g <- ggplotGrob(p)
                  px <- which(g$layout$name=="panel")
                  id <- grep("text", names(g$grobs[[px]]$children))
                  for(i in id)  g$grobs[[px]]$children[[i]]$gp$fontfamily <- fam
                  grid::grid.newpage()
                  grid::grid.draw(g)
                  invisible(g)
                  }

Plots

# original
plot(l28, plot.percents=TRUE, plot.percent.low = FALSE, 
                                             plot.percent.high = FALSE)
# with changed font
grid_fam(plot(l28, plot.percents=TRUE, plot.percent.low = FALSE, 
                                             plot.percent.high = FALSE))

There is most likely a simpler way to do this.


EDIT Update from comments: please feel free to improve

# initial plot
p <- plot(l28, plot.percents=TRUE, plot.percent.low = FALSE, 
                                             plot.percent.high = FALSE)

# Look at structure of returned ggplot - 
# it does not contain all the info used to generate the plot
str(p)

# g is a gtable which contains the grobs that make up the plot
g <- ggplotGrob(p)
g

# Get the list of parent grobs
g$grobs 

# layout details
g$layout

# we are interested in the grobs with layout name 'panel'
g1 <- g$grobs[[which(g$layout$name=="panel")]]

# have a look at the children within this gTree
childNames(g1)

# look at the structure - we are interested in the grobs with 
# name 'GRID.text.###'
# have a look at fontfamily and its position in the list structure
str(g1)

# extract the position of the grobs with names with 'text' in then
id <- grep("text", names(g$grobs[[which(g$layout$name=="panel")]]$children))

# check
childNames(g1)[id]

# look at grobs to be changed 
str(g$grobs[[which(g$layout$name=="panel")]]$children[id])

# loop through the text grobs changing the fonts
for(i in id)  g$grobs[[which(g$layout$name=="panel")]]$children[[i]]$gp$fontfamily <- "Georgia"

# plot grid obkects
grid::grid.newpage()
grid::grid.draw(g)

# the use of invisible returns the updated gtable if it assigned to a variable
out <- grid_fam(plot(l28, plot.percents=TRUE, plot.percent.low = FALSE, 
                     plot.percent.high = FALSE))

out

Upvotes: 2

Related Questions