r - 更改包内 geom_text 的字体系列

标签 r fonts ggplot2 package

使用名为 Likert 的 R 包,它使用 ggplot2,并且我想更改字体系列。在包中它看起来像这样:

    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)
    }

想知道如何在不了解很多此信息的情况下从包外部更改 geom_text 。对于标签,您可以只使用主题,但主题似乎不适用于此。

目前

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

将其他所有内容更改为乔治亚州。

最佳答案

重新评论,您可以编辑grobs来更改geom_text调用中的fontfamily

代码被包装在一个函数中,因为您想要复制图表。

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)
                  }

绘图

# 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))

很可能有一种更简单的方法来做到这一点。


编辑评论更新:请随时改进

# 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

关于r - 更改包内 geom_text 的字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27368583/

相关文章:

css - 使用 css 的自定义 css 仅适用于主页而非整个网站

r - ggplot 中的两个散点图,两个散点图之间有箭头

r - 计算向量在另一个向量中的出现次数

r - 使用 dplyr 汇总时省略 NA

css - @font-face CSS 属性无效

r - 如何在 R 中使用 ggplot2 制作默认自定义主题

r - R Shiny :删除ggplot2背景以使其透明

r - runif() 真的有文档中所述的范围 : 0<= runif(n) <= 1, 吗?

regex - r + 搜索字符串并返回值

ios - 设置自定义 UILabel