다시 말하면 을 편집하여 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
아마 쉬운 방법을 개선 주시기하지만이 같은 복제되지 않은 경우 당신은 확실히 행할은'grob' 수준 – user20650
에서 편집 할 수 있습니다하시기 바랍니다 다른 데이터로 50 회 그래프. – user3055869
솔직히이 시점에서 함수를 재정의 할 것입니다. [github repo] (https://github.com/jbryer/likert)를 포크하고 원하는대로 변경할 수 있습니다. – Gregor