更改位于包内的geom_text字体系列

用户名

使用一个名为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修改fontfamilygeom_text电话。

当您要复制图形时,代码被包装在一个函数中。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

更改位于包内的geom_text字体系列

来自分类Dev

更改EditText setError()的字体系列

来自分类Dev

fontconfig:无法更改字体系列

来自分类Dev

如何更改levelplot字体系列?

来自分类Dev

更改桌面的字体系列

来自分类Dev

根据语言更改字体系列

来自分类Dev

更改反应条纹元素的字体系列

来自分类Dev

无法在Wordpress中使用字体系列更改字体

来自分类Dev

动态更改字体系列和字体大小

来自分类Dev

使用HtmlAgilitPack更改字体系列和字体大小

来自分类Dev

使用CSS根据字体系列更改字体大小?

来自分类Dev

更改字体系列,重命名字体

来自分类Dev

仅更改RichTextBox字体系列,而不更改FontSize

来自分类Dev

更改数据表中的字体系列

来自分类Dev

Google Maps API更改了我的字体系列

来自分类Dev

Android Studio中的按钮更改字体系列

来自分类Dev

无法更改Boostrap“良好”组件中的字体系列

来自分类Dev

如何防止更改Matplotlib中的字体系列?

来自分类Dev

jQuery使用带引号的变量更改字体系列

来自分类Dev

使用fontconfig更改某种语言/脚本的字符的字体系列?

来自分类Dev

如何在Android中更改Tabhost的字体系列

来自分类Dev

更改字体系列后,Fittext不适用

来自分类Dev

在R中的交互图中更改字体系列

来自分类Dev

CKEditor-更改页面加载时的字体系列

来自分类Dev

无法更改 GXT 文本区域中的字体系列

来自分类Dev

根据其他 CSS 值更改字体系列

来自分类Dev

有条件地更改axis.text字体系列和字体大小会产生不必要的“间隙”

来自分类Dev

指定h1字体系列不会更改字体吗?

来自分类Dev

在不知道字体系列的情况下更改Canvas的字体大小

Related 相关文章

热门标签

归档