mac ggplot2中文保存问题解决

rm(list=ls())
options(warn = -1)
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(cowplot))
suppressPackageStartupMessages(library(gridExtra))
suppressPackageStartupMessages(library(png))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(kableExtra))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(vioplot))
suppressPackageStartupMessages(library(googleVis))
suppressPackageStartupMessages(library(reticulate))
suppressPackageStartupMessages(library(patchwork))
suppressPackageStartupMessages(library(egg))
suppressPackageStartupMessages(library(ggvis))

#############################################################################
old=theme_set(theme_bw()+theme(strip.background = element_rect(fill="white"),
                                         panel.background = element_blank(),
                                         panel.grid =element_blank()))

# 注意这个赋值语句,尽管这是一个赋值操作,看似对程序没有任何影响,但是这个语句其实起着ggplot的设置初始化的作用
# 不运行这句话,导致最终的图是灰色背景且有网格线的,需要特别注意
#############################################################################

options(repr.plot.width=12,repr.plot.height=5)
gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
op=par(mar=c(5,4,6,4))
color_celltype=c('#1CE6FF','#FF34FF','#FF4A46','#008941','#A30059','#FF2F80','#0000A6','#63FFAC','#004D43','#8FB0FF','#4FC601','#3B5DFF','#4A3B53','#61615A','#BA0900','#6B7900','#00C2A0','#FFAA92','#FF90C9','#B903AA')
image(1:length(color_celltype),1, as.matrix(1:length(color_celltype)),col=color_celltype,ylab="",xlab="",axes=F)
axis(3,at=seq(1:length(color_celltype)),labels=color_celltype,las=2,lwd=0)

options(repr.plot.width=12,repr.plot.height=5)
op=par(mar=c(5,4,6,4))
col.set.update <- c("#c10023", "#008e17", "#fb8500", "#f60000", "#FE0092", "#bc9000","#4ffc00", "#00bcac", "#0099cc",
                    "#D35400", "#00eefd", "#cf6bd6", "#99cc00", "#aa00ff", "#ff00ff", "#0053c8",
                    "#f2a287","#ffb3ff", "#800000", "#77a7b7", "#00896e", "#00cc99", "#007CC8")
image(1:length(col.set.update),1, as.matrix(1:length(col.set.update)),col=col.set.update,ylab="",xlab="",axes=F)
axis(3,at=seq(1:length(col.set.update)),labels=col.set.update,las=2,lwd=0)

library(readxl)
options(repr.plot.width=10,repr.plot.height=5)
file_path="../comparison.xlsx"
data_para_hvg= read_xlsx(file_path,"para_hvg")
data = data_para_hvg[2:8,10:12]
colnames(data)=c("HVG","NMI","ARI")
data = data[-c(1),]
data = data.frame(data)
data_NMI = data[,c(1,2)]
data_NMI["type"] = "NMI"
colnames(data_NMI)[2] = "value"
#print(data_NMI)

data_ARI = data[,c(1,3)]
data_ARI["type"] = "ARI"
colnames(data_ARI)[2] = "value"
#print(data_ARI)

data = rbind(data_NMI,data_ARI)
data$value = as.numeric(data$value)
shapes <- c(17, 15)  # 不同的形状代码

# 绘图并突出显示x轴和y轴
p = ggplot(data, aes(x = HVG, y = value, group = type, color = type)) +
  geom_line(aes(linetype = type)) +  # 线型基于方法变量
  geom_point(aes(shape = type)) +  # 点形状基于方法变量
  theme_minimal() +
  theme(
    legend.title = element_blank(),
    legend.position = "right", 
    plot.title = element_text(hjust = 0.5),  # 标题居中
    axis.line = element_line(size = 0.5), # 突出显示x轴和y轴
    text = element_text(family='STHeiti')  # 显示中文字体,text = element_text(family='Kai')也是可以的
  ) +
  labs(title = "高变基因数目的敏感性分析",
       x = "HVG(高变基因)数目",
       y = "指标值") +
  scale_color_manual(values = c("NMI" = "red", "ARI" = "blue")) +
  scale_shape_manual(values = shapes) +
  scale_linetype_manual(values = c("NMI" = "solid", "ARI" = "twodash")
                       )
print(p)
# 如果你需要保存图表,可以使用以下代码
# ggsave("./merge_bct_umap.pdf",pa,width=18,height = 10,dpi=600)
ggsave("./paramter_HVG.pdf",p, width = 10, height = 6, dpi = 600)
#
#Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : 字体类别出错
#Traceback:

#1. ggsave("./paramter_HVG.pdf", p, width = 10, height = 6, dpi = 600)

结果如下

解决办法

library(showtext)
font_add("songti","Songti.ttc")
#font_add('Arial','/Library/Fonts/Arial.ttf') #加载字体,MAC 中字体库在 /Library/Fonts
showtext_auto()

这里注意我的电脑显示/Library/Fonts/的目录内容如下

library(readxl)
options(repr.plot.width=10,repr.plot.height=5)
file_path="../comparison.xlsx"
data_para_hvg= read_xlsx(file_path,"para_hvg")
data = data_para_hvg[2:8,10:12]
colnames(data)=c("HVG","NMI","ARI")
data = data[-c(1),]
data = data.frame(data)
data_NMI = data[,c(1,2)]
data_NMI["type"] = "NMI"
colnames(data_NMI)[2] = "value"
#print(data_NMI)

data_ARI = data[,c(1,3)]
data_ARI["type"] = "ARI"
colnames(data_ARI)[2] = "value"
#print(data_ARI)

data = rbind(data_NMI,data_ARI)
data$value = as.numeric(data$value)
shapes <- c(17, 15)  # 不同的形状代码

# 绘图并突出显示x轴和y轴
p = ggplot(data, aes(x = HVG, y = value, group = type, color = type)) +
  geom_line(aes(linetype = type)) +  # 线型基于方法变量
  geom_point(aes(shape = type)) +  # 点形状基于方法变量
  theme_minimal() +
  theme(
    legend.title = element_blank(),
    legend.position = "right", 
    plot.title = element_text(hjust = 0.5),  # 标题居中
    axis.line = element_line(size = 0.5), # 突出显示x轴和y轴
    text = element_text(family='STHeiti')  # 显示中文字体,text = element_text(family='Kai')也是可以的
  ) +
  labs(
       x = "HVG(高变基因)数目",
       y = "指标值") +
  scale_color_manual(values = c("NMI" = "red", "ARI" = "blue")) +
  scale_shape_manual(values = shapes) +
  scale_linetype_manual(values = c("NMI" = "solid", "ARI" = "twodash")
                       )
print(p)
# 如果你需要保存图表,可以使用以下代码
# ggsave("./merge_bct_umap.pdf",pa,width=18,height = 10,dpi=600)
ggsave("./HVG敏感性分析.pdf",p, width = 10, height = 6, dpi = 600)
#
#Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : 字体类别出错
#Traceback:

#1. ggsave("./paramter_HVG.pdf", p, width = 10, height = 6, dpi = 600)

注意这个地方我在画图中使用的是

text = element_text(family='STHeiti')

这个是宋体和黑体,不过这个没报错,是可以正常保存的,

相关推荐
hackeroink2 小时前
【2024版】最新推荐好用的XSS漏洞扫描利用工具_xss扫描工具
前端·xss
迷雾漫步者3 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
SoraLuna3 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
向前看-3 小时前
验证码机制
前端·后端
阿7_QuQ4 小时前
怎么在Windows上远程控制Mac电脑?
macos
燃先生._.5 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
高山我梦口香糖5 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
m0_748235246 小时前
前端实现获取后端返回的文件流并下载
前端·状态模式
m0_748240256 小时前
前端如何检测用户登录状态是否过期
前端