※ 引述《ruokcnn (Dean)》之铭言:
: [问题类型]:
: 程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
: [软件熟悉度]:
: 入门(写过其他程式,只是对语法不熟悉)
: [问题叙述]:
: 为了ggplot抗锯齿输出 ,小弟找到可以用type = "cairo"包在ggsave里面
: 但只要有包原本嵌好的字型在存的时候就会被打回新细明体
: 令人好不伤心
: 想请问有没有一兼二顾,摸蛤仔兼洗裤的方式呢?
: 还是只能把dpi调爆高看起来比较没锯齿?
: [程式范例]:
: [环境叙述]:
: R3.3.3
: [关键字]:
: GGplot
: cairo
: 字型
Linux:
# centos 7 要先yum install -y google-noto-cjk-fonts
library(ggplot2)
library(Cairo)
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
g <- ggplot(df, aes(gp, y)) + geom_point() + labs(title = "中文测试") +
theme(text = element_text(family = "Noto Sans CJK TC"))
ggsave("tmp1.png", g)
# https://imgur.com/f5fALCa
ggsave("tmp2.png", g, type = "cairo")
# https://imgur.com/3LtezUk
Windows:
library(ggplot2)
library(Cairo)
library(showtext)
showtext.auto()
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
g <- ggplot(df, aes(gp, y)) + geom_point() + labs(title = "中文测试") +
theme(text = element_text(family = "Noto Sans CJK TC"))
ggsave("tmp1.png", g)
# https://imgur.com/T9XGpxd
ggsave("tmp2.png", g, type = "cairo")
# https://imgur.com/1FRfOWU
# windows正常情况:
windowsFonts("Noto Sans CJK TC Regular" =
windowsFont("Noto Sans CJK TC Regular"))
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
g <- ggplot(df, aes(gp, y)) + geom_point() + labs(title = "中文测试") +
theme(text = element_text(family = "Noto Sans CJK TC Regular"))
ggsave("tmp3.png", g)
# https://imgur.com/7Z56rCj