6

次のようなデータセットがあるとします。

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

パッケージのTradeGothic LT CondEighteenフォントを使用して、ggplot でプロットできます。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")

ここに画像の説明を入力

しかしggplot_gtable、プロットで使用すると:

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

grid.draw 関数を実行すると、エラーが発生します。エラーは次のとおりです。

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

TradeGothic LT CondEighteenフォントを使用しない場合、エラーは発生しません。

ご協力いただきありがとうございます!

4

1 に答える 1

7

私は、ええと、このフォントのコピーを「たまたま持って」いて、extrafont::font_import()ダンスをしたところ、エラーが発生しました。しかし、さらに調べてみると、次のようになります。

library(purrr)

loadfonts()

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

PDFデバイスがこの名前を望んでいるようです。

R には多くの優れた機能がありますが、フォントの処理方法は、トカゲのカラス (#atla) と同じくらい微妙で親しみやすく、使いやすいものです。

アップデート

完全な例 (破損したdata.frame作成コードと vs の参照plotなしで、p実際にvsを実行grid.draw()するか、ggplot gtable を変更した後:gtplotp

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

その ^^ 部分により、次の PDF が作成されます (Preview から PNG としてエクスポートされます)。

ここに画像の説明を入力

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

その ^^ 部分により、次の PDF が作成されます (Preview から PNG としてエクスポートされます)。

ここに画像の説明を入力

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)

これは RStudio からのスクリーン グラブです (RStudio グラフィックス デバイスにあることを示すために、RStudio UI のビットが含まれています)。

ここに画像の説明を入力

これをしなければならないのは悲しいことです (フォントの命名規則を変更して、R プレゼンテーション コードのさまざまなビットが正しいものを選択できるようにします) が、それが R のフォントの現在の状態です。

私は会社の調査レポート用のコンテンツを生成し、画面 (開発中) とプロダクション PDF 生成 (クリエイティブ チームへの最終出力ハンドオフ用) 用に個別のテーマ コードを生成します。それはイライラする松葉杖ですが、うまくいきます。

于 2017-01-01T05:55:44.393 に答える