1. script文件含中文
File -> Reopen with encoding… -> utf-8
重新打开后,script中的中文可以正常显示
2. csv文件含中文
Sys.setlocale("LC_CTYPE", "UTF-8")
setwd("/Users/-/Desktop") #file path
# case1: encoding by UTF-8
data <- read.csv("data.csv",fileEncoding="UTF-8")
# case2: encoding by GBK
data <- read.csv("data.csv",fileEncoding="UTF-8")
p.s. 一般都是用utf-8编码的,但有时候从windows拷贝过来,在mac下打开的csv文件往往是GBK编码
附: 如果用excel打开含中文字符的csv显示为乱码的话,有以下两种方式:
method 1:file -> 打开方式 -> 文本编辑
method 2:对原csv文件进行编码转换,参见http://blog.sina.com.cn/s/blog_69e225900102w2mc.html
3. ggplot2画图含中文
如何在RStudio中使用自己电脑里的字体: