2017-01-01 45 views
5

Say mam zestaw danych tak:Korzystanie czcionkę z extrafont w grid.draw

dat <- data.frame 
    text = c(
    "It made me feel very positive to brand X", 
    "It was clear and easy to understand", 
    "I didn't like it al all"), 
    value=runif(3) 
) 

mogę wykreślić ją w ggplot używając Czy TradeGothic LT CondEighteen czcionki z pakietu extrafonts:

library(ggplot2) 
p <- ggplot(dat, aes(text, value)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    labs(title="  Do you agree with the following statements?")+ 
    theme_bw(16)+ 
    theme(text=element_text(family="TradeGothic LT CondEighteen")) 

ggsave('plot.pdf', plot = plot, path = "/Users/jacobdeecurtis/Desktop") 

enter image description here

Ale gdy używam ggplot_gtable na działce:

gt <- ggplot_gtable(ggplot_build(plot)) 
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1,   max(gt$layout$r)) 
grid::grid.draw(plot) 

ggsave('plot.pdf', plot = plot, path = "/Users/jacobdeecurtis/Desktop") 

Po uruchomieniu funkcji grid.draw pojawia się błąd. Błąd jest:

Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : 
    polygon edge not found 
In addition: Warning messages: 
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : 
    no font could be found for family "TradeGothic LT CondEighteen"... 

nie pojawia się błąd, kiedy nie jest używana TradeGothic LT CondEighteen czcionki .

Dzięki za pomoc!

+0

Spróbuj '" TradeGothic LT CondEighteen "' – hrbrmstr

+0

Interesujące. Czy tego nie próbowałem? Jak to się różni? –

+0

Czy zrobiłeś 'extrafont :: loadfonts()'? – hrbrmstr

Odpowiedz

6

Ja, uh, "akurat miałem" kopię tej czcionki i wykonałem taniec extrafont::font_import() i otrzymałem błąd. Ale po dalszej kontroli:

library(purrr) 

loadfonts() 

pdfFonts() %>% 
    map_chr("family") %>% 
    keep(~grepl(".*Trade.*", .)) 
## [1] "TradeGothic LT CondEighteen" 

wydaje się, że urządzenie PDF chce tego imienia.

Podczas gdy R ma mnóstwo wspaniałości, sposób, w jaki traktuje czcionki, jest tak samo dopracowany, przyjazny i użyteczny jak wrona jaszczurki (#atla).

UPDATE

Kompletny przykład (bez kodu łamanego data.frame tworzenia i przedstawieniu plot vs p i faktycznie grid.draw() ing gt vs plot lub p po zmodyfikowaniu gtable ggplot:

library(ggplot2) 
library(grid) 
library(extrafont) 

dat <- data.frame(
    text = c(
    "It made me feel very positive to brand X", 
    "It was clear and easy to understand", 
    "I didn't like it al all"), 
    value=runif(3) 
) 

p <- ggplot(dat, aes(text, value)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    labs(title="  Do you agree with the following statements?")+ 
    theme_bw(16)+ 
    theme(text=element_text(family="TradeGothic LT CondEighteen")) 

loadfonts() 

ggsave('plot.pdf', plot=p, path="~/Desktop") 

To ^^ część tworzy następujący plik PDF (eksportowany z podglądu jako PNG):

enter image description here

gt <- ggplot_gtable(ggplot_build(p)) 
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r)) 

pdf("~/Desktop/plot.pdf") 
grid::grid.draw(gt) 
dev.off() 

To ^^ część składa następujące PDF (wyeksportowany z podglądu jako PNG):

enter image description here

p <- ggplot(dat, aes(text, value)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    labs(title="  Do you agree with the following statements?")+ 
    theme_bw(16)+ 
    theme(text=element_text(family="TradeGothicLT-CondEighteen")) 

gt <- ggplot_gtable(ggplot_build(p)) 
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r)) 

grid::grid.draw(gt) 

Oto grab ekran z RStudio (z kawałkami RStudio Interfejs użytkownika dołączony w celu pokazania go w urządzeniu graficznym RStudio):

enter image description here

To smutne, że musimy to zrobić (zmienić konwencje nazewnictwa dla czcionki, aby różne fragmenty kodu prezentacji R mogły wybrać właściwą), ale to jest obecny stan czcionek w R.

Generuję treść raportów badawczych dla mojej firmy i oddzielny kod tematyczny dla ekranu (podczas tworzenia) i produkcji generowanego pliku PDF (ostateczne przekazywanie do zespołu kreatywnego). To frustrujące kule, ale działa.

+0

Tak, teraz wszystko działa. Dziękuję Ci! –