我们平时在使用ggplot绘图时,如果绘图标题、坐标轴标题或者文本标签中含有中文,在绘图结果中将显示为方框。 如:
ggplot(data = new_a, aes(x = 月, y = ratio, color = 年份, group = 年份)) + geom_line() + geom_point() + guides(color = guide_legend(title = "年份")) + xlab('2017-2019年流感分月发病情况') +ylab('发病率')+ labs(title = '流感') + theme_classic() + theme(plot.title = element_text(hjust = 0.5))
image.png
解决办法为在主题中添加字体,如 theme_classic()中添加base_family = "STKaiti",即theme_classic(base_family = "STKaiti")
ggplot(data = new_a, aes(x = 月, y = ratio, color = 年份, group = 年份)) + geom_line() + geom_point() + guides(color = guide_legend(title = "年份")) + xlab('2017-2019年流感分月发病情况') +ylab('发病率')+ labs(title = '流感') + theme_classic(base_family = "STKaiti") + theme(plot.title = element_text(hjust = 0.5))
image.png
如果使用默认的绘图系统,使用par()函数更改默认绘图参数并指定一种中文字体即可: par(family="STKaiti")
欢迎关注微信公众号;生信编程日常