2014-12-08 1 views
1

Likert라는 g 패키지를 ggplot2를 사용하는 R 패키지를 사용하여 폰트 패밀리를 변경하려고합니다. 패키지에서 그것은 다음과 같습니다패키지 안에있는 geom_text의 글꼴 패밀리 변경

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

조지아에 다른 모든 것들을 변경합니다.

+0

아마 쉬운 방법을 개선 주시기하지만이 같은 복제되지 않은 경우 당신은 확실히 행할은'grob' 수준 – user20650

+0

에서 편집 할 수 있습니다하시기 바랍니다 다른 데이터로 50 회 그래프. – user3055869

+0

솔직히이 시점에서 함수를 재정의 할 것입니다. [github repo] (https://github.com/jbryer/likert)를 포크하고 원하는대로 변경할 수 있습니다. – Gregor

답변

2

다시 말하면 을 편집하여 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 
+0

우아하지는 않지만 잘 작동합니다. 고마워. 댓글에서 기능이 어떻게 작동하는지 간단히 설명해 주시겠습니까? R 초보자로서 나를 혼란스럽게합니다. – user3055869

+0

@ user3055869; 내 제한된 이해로 업데이 트되었습니다 ... hth – user20650

+1

하지만 나는 위의 Gregor에 동의하고, 이것은 그것에 대해 갈 길이 멀다. 그러나, 나는 아마도 내 자신의 ggplot을 만들 것인데, 이는 repo를 포기하기보다는 너무 어려워서는 안된다. – user20650

관련 문제