hrbrmstr / hrbrthemes

:lock_with_ink_pen: Opinionated, typographic-centric ggplot2 themes and theme components
Other
1.23k stars 96 forks source link

The plot does't work for Chinese font family #32

Closed JiaxiangBU closed 5 years ago

JiaxiangBU commented 6 years ago

theme(text = element_text(family = "STKaiti")) is a way to show Chinese font on the plot. I find it cannot work with function theme_ipsum().

> library(hrbrthemes)
> library(gcookbook)
> library(tidyverse)
> 
> # current verison
> packageVersion("hrbrthemes")
[1] ‘0.5.0’
> ggplot(mtcars, aes(mpg, wt)) +
+   geom_point() +
+   labs(x="燃油效率", y="重量,单位吨",
+        title="散点图例子",
+        subtitle="就是用用中文而已",
+        caption="数据来自tidyverse, mtcars") +
+   theme_ipsum() +
+   theme(text = element_text(family = "STKaiti"))


But for other ggplot theme, like theme_minimal(), this way works well.

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x="燃油效率", y="重量,单位吨",
       title="散点图例子",
       subtitle="就是用用中文而已",
       caption="数据来自tidyverse, mtcars") +
  # theme_ipsum() +
  theme_minimal() +
  theme(text = element_text(family = "STKaiti"))
  ggsave("theme_minimal.png",device = "png")


Here is my sessionInfo()

> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.3.0    stringr_1.3.0    dplyr_0.7.4      purrr_0.2.4     
 [5] readr_1.1.1      tidyr_0.8.0      tibble_1.4.2     ggplot2_2.2.1   
 [9] tidyverse_1.2.1  gcookbook_1.0    hrbrthemes_0.5.0 optparse_1.4.4  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     lubridate_1.7.3  lattice_0.20-35  assertthat_0.2.0
 [5] rprojroot_1.2    digest_0.6.12    psych_1.7.8      mime_0.5        
 [9] R6_2.2.2         cellranger_1.1.0 plyr_1.8.4       backports_1.1.0 
[13] reprex_0.1.2     evaluate_0.10.1  httr_1.3.1       pillar_1.2.1    
[17] rlang_0.2.0      lazyeval_0.2.1   readxl_1.0.0     miniUI_0.1.1    
[21] rstudioapi_0.7   extrafontdb_1.0  rmarkdown_1.9    labeling_0.3    
[25] extrafont_0.17   foreign_0.8-69   munsell_0.4.3    shiny_1.0.5     
[29] broom_0.4.3      compiler_3.4.4   httpuv_1.3.6.2   modelr_0.1.1    
[33] pkgconfig_2.0.1  mnormt_1.5-5     htmltools_0.3.6  crayon_1.3.4    
[37] grid_3.4.4       nlme_3.1-131.1   jsonlite_1.5     xtable_1.8-2    
[41] Rttf2pt1_1.3.6   gtable_0.2.0     magrittr_1.5     scales_0.5.0    
[45] cli_1.0.0        stringi_1.1.7    reshape2_1.4.3   bindrcpp_0.2    
[49] getopt_1.20.2    xml2_1.2.0       tools_3.4.4      glue_1.2.0      
[53] hms_0.4.2        parallel_3.4.4   yaml_2.1.18      colorspace_1.3-2
[57] rvest_0.3.2      knitr_1.20       bindr_0.1        haven_1.1.1 
r-stata commented 5 years ago

老哥,你可以用theme_ipsum(base_family = "包含中文的字体"),这样就行了

JiaxiangBU commented 5 years ago

我尝试了,依然是报错的。

library(hrbrthemes)
#> Warning: 程辑包'hrbrthemes'是用R版本3.5.2 来建造的
#> NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
#>       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
#>       if Arial Narrow is not on your system, please see http://bit.ly/arialnarrow
library(gcookbook)
library(tidyverse)

# current verison
packageVersion("hrbrthemes")
#> [1] '0.6.0'

Created on 2019-02-06 by the reprex package (v0.2.1)

p <- 
    ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    labs(x="燃油效率", y="重量,单位吨",
         title="散点图例子",
         subtitle="就是用用中文而已",
         caption="数据来自tidyverse, mtcars")
p + theme_ipsum()

image

p + theme_ipsum(base_family = "STKaiti")
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : 找不到多边形的边

这个指定的字体我也是有的,如图。

image

r-stata commented 5 years ago

老哥,你的例子我尝试了,没有出错,你把绘图窗口调大点试试呗:

library(hrbrthemes)
library(gcookbook)
library(tidyverse)
packageVersion("hrbrthemes")

p <- 
  ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x="燃油效率", y="重量,单位吨",
       title="散点图例子",
       subtitle="就是用用中文而已",
       caption="数据来自tidyverse, mtcars")
p + theme_ipsum()

image

p + theme_ipsum(base_family = "STKaiti")

image

JiaxiangBU commented 5 years ago

@czxa 我把窗口放大到这个程度也不行,感觉是我电脑配置的问题了。

r-stata commented 5 years ago

@JiaxiangBU 是不是因为你的R版本过老呢?要不你升级一下R的版本再试试。

r-stata commented 5 years ago

@JiaxiangBU 我的R版本信息是:

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/en_US.UTF-8/zh_CN.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] mathpix_0.3.0      reshape2_1.4.3     RColorBrewer_1.1-2
[4] dplyr_0.7.8        ggplot2_3.1.0     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0       rstudioapi_0.9.0 bindr_0.1.1     
 [4] magrittr_1.5     tidyselect_0.2.5 munsell_0.5.0   
 [7] colorspace_1.4-0 R6_2.3.0         rlang_0.3.1     
[10] httr_1.4.0       stringr_1.4.0    plyr_1.8.4      
[13] tools_3.5.2      grid_3.5.2       gtable_0.2.0    
[16] withr_2.1.2      lazyeval_0.2.1   assertthat_0.2.0
[19] tibble_2.0.1     crayon_1.3.4     bindrcpp_0.2.2  
[22] purrr_0.3.0      base64enc_0.1-3  curl_3.3        
[25] glue_1.3.0       stringi_1.2.4    compiler_3.5.2  
[28] pillar_1.3.1     scales_1.0.0     jsonlite_1.6    
[31] pkgconfig_2.0.2 
JiaxiangBU commented 5 years ago

我的R不是最新的,那我升级下看看。

"session info"
#> [1] "session info"

Created on 2019-02-12 by the reprex package (v0.2.1)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.5.1 (2018-07-02) #> os macOS 10.14.1 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate zh_CN.UTF-8 #> ctype zh_CN.UTF-8 #> tz Asia/Shanghai #> date 2019-02-12 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.0) #> backports 1.1.2 2017-12-13 [1] CRAN (R 3.5.0) #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.5.0) #> callr 3.0.0 2018-08-24 [1] CRAN (R 3.5.0) #> cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.0) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.0) #> devtools 2.0.1 2018-10-26 [1] CRAN (R 3.5.1) #> digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.0) #> evaluate 0.12 2018-10-09 [1] CRAN (R 3.5.0) #> fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.0) #> glue 1.3.0 2018-07-17 [1] CRAN (R 3.5.0) #> htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.5.0) #> knitr 1.20 2018-02-20 [1] CRAN (R 3.5.0) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.0) #> pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.0) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.0) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.0) #> processx 3.2.0 2018-08-16 [1] CRAN (R 3.5.0) #> ps 1.2.1 2018-11-06 [1] CRAN (R 3.5.0) #> R6 2.3.0 2018-10-04 [1] CRAN (R 3.5.0) #> Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.0) #> remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.0) #> rlang 0.3.0.1 2018-10-25 [1] CRAN (R 3.5.0) #> rmarkdown 1.10 2018-06-11 [1] CRAN (R 3.5.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.0) #> stringi 1.2.4 2018-07-20 [1] CRAN (R 3.5.0) #> stringr 1.3.1 2018-05-10 [1] CRAN (R 3.5.0) #> testthat 2.0.0 2017-12-13 [1] CRAN (R 3.5.0) #> usethis 1.4.0 2018-08-14 [1] CRAN (R 3.5.0) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.0) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.5.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library ```
JiaxiangBU commented 5 years ago
library(hrbrthemes)
#> NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
#>       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
#>       if Arial Narrow is not on your system, please see http://bit.ly/arialnarrow
library(ggplot2)
p <- 
    ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    labs(x="燃油效率", y="重量,单位吨",
         title="散点图例子",
         subtitle="就是用用中文而已",
         caption="数据来自tidyverse, mtcars")
p + theme_ipsum()

p + theme_ipsum(base_family = "STKaiti")

Created on 2019-04-20 by the reprex package (v0.2.1)