中文文本与ggplot2和rmarkdown

时间:2017-09-30 00:26:01

标签: r ggplot2 r-markdown chinese-locale

在较旧的R版本中,我能够用中文书写并插入字符标题,轴等字符,如ggtitle("你好")

现在我更新到R 3.4.1并且不再起作用了,所以我读到了extrafontshowtext。 OS X和Windows中的两个选项部分直接来自R脚本(一些缺少的字符),但Rmd文档根本不起作用。

你如何绕过这个问题?

以下是我在下载和安装Google的Noto Sans(中文版)后尝试的一个虚拟示例:

R脚本

library(readr)
library(dplyr)
library(ggplot2)
library(forcats)
library(showtext)
font_add("notosans","C:\\Windows\\Fonts\\notosansCJK-REGULAR.TTC")
showtext_auto()

data6 <- 'product,year,exports
"<U+6C34><U+679C>",2006,12.7738970275796
"<U+6C34><U+679C>",2007,29.3653657256263
"<U+6C34><U+679C>",2008,39.316127658655
"<U+6C34><U+679C>",2009,39.0761304418969
"<U+6C34><U+679C>",2010,75.6940602302296
"<U+6C34><U+679C>",2011,177.820219571275
"<U+6C34><U+679C>",2012,365.473524370339
"<U+6C34><U+679C>",2013,510.749915592455
"<U+6C34><U+679C>",2014,728.081296787781
"<U+6C34><U+679C>",2015,700
"<U+518D><U+5236>",2006,253.47905480396
"<U+518D><U+5236>",2007,264.225834261596
"<U+518D><U+5236>",2008,326.136778583744
"<U+518D><U+5236>",2009,427.940739616724
"<U+518D><U+5236>",2010,323.657699512519
"<U+518D><U+5236>",2011,432.975616101368
"<U+518D><U+5236>",2012,396.086992479141
"<U+518D><U+5236>",2013,436.591823128008
"<U+518D><U+5236>",2014,447.010172101558
"<U+518D><U+5236>",2015,422
"<U+74F6><U+88C5><U+9152>",2006,4.95358050561666
"<U+74F6><U+88C5><U+9152>",2007,8.96083067675526
"<U+74F6><U+88C5><U+9152>",2008,13.8090506134479
"<U+74F6><U+88C5><U+9152>",2009,21.9872388536114
"<U+74F6><U+88C5><U+9152>",2010,37.9287803581076
"<U+74F6><U+88C5><U+9152>",2011,66.8964990028785
"<U+74F6><U+88C5><U+9152>",2012,83.4418004381831
"<U+74F6><U+88C5><U+9152>",2013,92.9214432451592
"<U+74F6><U+88C5><U+9152>",2014,112.026030483586
"<U+74F6><U+88C5><U+9152>",2015,165
"<U+9C52><U+5C6C>",2006,22.688976839089
"<U+9C52><U+5C6C>",2007,11.411605056316
"<U+9C52><U+5C6C>",2008,23.9390555779806
"<U+9C52><U+5C6C>",2009,40.4951163194693
"<U+9C52><U+5C6C>",2010,23.7351577103601
"<U+9C52><U+5C6C>",2011,44.0371844236012
"<U+9C52><U+5C6C>",2012,33.7428527214565
"<U+9C52><U+5C6C>",2013,61.6311397997178
"<U+9C52><U+5C6C>",2014,111.132172291715
"<U+9C52><U+5C6C>",2015,87
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2006,30.0997310483352
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2007,32.3756614202348
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2008,33.7104288388731
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2009,38.7860232819799
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2010,60.6322436480363
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2011,108.208487604708
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2012,129.281398927209
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2013,203.120829283752
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2014,250.524648521889
"<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2015,196
'

data6 <- read_csv(data6) %>% 
  mutate(product = gsub('"','',product),
         product = gsub('<','\\\\',product),
         product = gsub('>','',product),
         product = gsub('\\+','',product))

g6 = ggplot() + 
  geom_area(aes(y = exports, x = year, fill = fct_rev(product)), data = data6, stat="identity") + 
  xlab("年") + ylab("百万美元") + 
  scale_x_continuous(breaks=seq(2000,2015,3)) + 
  scale_y_continuous(breaks=seq(0,1600,400)) + 
  theme(panel.background = element_rect(fill="white"), panel.grid = element_line(colour="white")) + 
  scale_fill_brewer(palette="Paired") + 
  theme(legend.position="bottom", legend.direction="horizontal", legend.title = element_blank(), axis.line.x = element_line(size=0.5), axis.line.y = element_line(size=0.5)) + 
  ggtitle("无铜矿或造纸木材出口中国",
          subtitle = "消息灵通人士: 智利海关总署, 智利中央银行 & 商务处智利中国") + 
  theme(plot.title=element_text(size=20, family="notosans"), plot.subtitle=element_text(size=12, family="notosans"), text=element_text(size=18, family="notosans"))
g6

enter image description here

R Markdown

---
title: "DUMMY EXAMPLE"
output: html_document
---

```{r setup, cache = FALSE, echo = FALSE, message = FALSE, warning = FALSE, tidy = FALSE}
knitr::opts_chunk$set(message = F, error = F, warning = F, cache = T, comment = NA, fig.align = 'center', dpi = 100, fig.width=7, fig.height=5, tidy = F, cache.path = '.dummy_example/', fig.path = 'dummy_example/', dev = 'png')
```

```{r}
library(readr)
library(dplyr)
library(ggplot2)
library(forcats)
library(showtext)
font_add("notosans","C:\\Windows\\Fonts\\notosansCJK-REGULAR.TTC")
showtext_auto()

data6 <- 'product,year,exports
  "<U+6C34><U+679C>",2006,12.7738970275796
  "<U+6C34><U+679C>",2007,29.3653657256263
  "<U+6C34><U+679C>",2008,39.316127658655
  "<U+6C34><U+679C>",2009,39.0761304418969
  "<U+6C34><U+679C>",2010,75.6940602302296
  "<U+6C34><U+679C>",2011,177.820219571275
  "<U+6C34><U+679C>",2012,365.473524370339
  "<U+6C34><U+679C>",2013,510.749915592455
  "<U+6C34><U+679C>",2014,728.081296787781
  "<U+6C34><U+679C>",2015,700
  "<U+518D><U+5236>",2006,253.47905480396
  "<U+518D><U+5236>",2007,264.225834261596
  "<U+518D><U+5236>",2008,326.136778583744
  "<U+518D><U+5236>",2009,427.940739616724
  "<U+518D><U+5236>",2010,323.657699512519
  "<U+518D><U+5236>",2011,432.975616101368
  "<U+518D><U+5236>",2012,396.086992479141
  "<U+518D><U+5236>",2013,436.591823128008
  "<U+518D><U+5236>",2014,447.010172101558
  "<U+518D><U+5236>",2015,422
  "<U+74F6><U+88C5><U+9152>",2006,4.95358050561666
  "<U+74F6><U+88C5><U+9152>",2007,8.96083067675526
  "<U+74F6><U+88C5><U+9152>",2008,13.8090506134479
  "<U+74F6><U+88C5><U+9152>",2009,21.9872388536114
  "<U+74F6><U+88C5><U+9152>",2010,37.9287803581076
  "<U+74F6><U+88C5><U+9152>",2011,66.8964990028785
  "<U+74F6><U+88C5><U+9152>",2012,83.4418004381831
  "<U+74F6><U+88C5><U+9152>",2013,92.9214432451592
  "<U+74F6><U+88C5><U+9152>",2014,112.026030483586
  "<U+74F6><U+88C5><U+9152>",2015,165
  "<U+9C52><U+5C6C>",2006,22.688976839089
  "<U+9C52><U+5C6C>",2007,11.411605056316
  "<U+9C52><U+5C6C>",2008,23.9390555779806
  "<U+9C52><U+5C6C>",2009,40.4951163194693
  "<U+9C52><U+5C6C>",2010,23.7351577103601
  "<U+9C52><U+5C6C>",2011,44.0371844236012
  "<U+9C52><U+5C6C>",2012,33.7428527214565
  "<U+9C52><U+5C6C>",2013,61.6311397997178
  "<U+9C52><U+5C6C>",2014,111.132172291715
  "<U+9C52><U+5C6C>",2015,87
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2006,30.0997310483352
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2007,32.3756614202348
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2008,33.7104288388731
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2009,38.7860232819799
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2010,60.6322436480363
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2011,108.208487604708
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2012,129.281398927209
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2013,203.120829283752
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2014,250.524648521889
  "<U+6797><U+4E1A><U+548C><U+6728><U+5BB6><U+5177>",2015,196
'

data6 <- read_csv(data6) %>% 
  mutate(product = gsub('"','',product),
         product = gsub('<','\\\\',product),
         product = gsub('>','',product),
         product = gsub('\\+','',product))

g6 = ggplot() + 
  geom_area(aes(y = exports, x = year, fill = fct_rev(product)), data = data6, stat="identity") + 
  xlab("年") + ylab("百万美元") + 
  scale_x_continuous(breaks=seq(2000,2015,3)) + 
  scale_y_continuous(breaks=seq(0,1600,400)) + 
  theme(panel.background = element_rect(fill="white"), panel.grid = element_line(colour="white")) + 
  scale_fill_brewer(palette="Paired") + 
  theme(legend.position="bottom", legend.direction="horizontal", legend.title = element_blank(), axis.line.x = element_line(size=0.5), axis.line.y = element_line(size=0.5)) + 
  ggtitle("无铜矿或造纸木材出口中国",
          subtitle = "消息灵通人士: 智利海关总署, 智利中央银行 & 商务处智利中国") + 
  theme(plot.title=element_text(size=20, family="notosans"), plot.subtitle=element_text(size=12, family="notosans"), text=element_text(size=18, family="notosans"))
g6
```

enter image description here

0 个答案:

没有答案