R ggplot2在图例中给出了重音

R ggplot2在图例中给出了重音,r,function,ggplot2,special-characters,legend,R,Function,Ggplot2,Special Characters,Legend,我创建了一个函数,以在线形图中绘制每个城市的一些数据。我希望用户能够更改图例中每个城市的标签 一个简化的例子: example_plot <- function(plot_labs = c("Anvers", "Liège")){ graphics.off() input <- data.table(x_axis = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5), y_axis = c(5, 6, 4, 2, 8, 9, 3,

我创建了一个函数,以在线形图中绘制每个城市的一些数据。我希望用户能够更改图例中每个城市的标签

一个简化的例子:

example_plot <- function(plot_labs = c("Anvers", "Liège")){

graphics.off()

input <- data.table(x_axis = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5), 
                y_axis = c(5, 6, 4, 2, 8, 9, 3, 1, 7, 5),
                City = c("Anvers", "Anvers", "Anvers", "Anvers", "Anvers", 
                         "Liege", "Liege", "Liege", "Liege", "Liege"))

ggplot(data = input, aes(x = x_axis, y = y_axis, group = City, lty = City)) + 
geom_line() + scale_linetype_manual(labels = plot_labs, breaks = c("Anvers", 
"Liege"), values = 1:2)
}

如果我使用plot_labs参数调用函数,它将正确显示:

example_plot(plot_labs = c("Anvers", "Liège"))

我发现更奇怪的是,如果我在命令提示符中复制粘贴函数的代码(而不是“source(example_plot.R)),那么一切都可以正常工作


知道为什么保存函数时它的行为会不同吗?

您可能正在以UTF-8之类的编码保存源文件,然后重新打开它,或者假设它是拉丁语-1


如果您使用的是RStudio,请检查“文件/使用编码保存”和“文件/使用编码重新打开”菜单点,并确保字符编码匹配。

这可能与文件的保存方式有关,如utf8或asciiGreat!非常感谢。source(“example_plot.R”,encoding=“utf-8”)有效。
example_plot(plot_labs = c("Anvers", "Liège"))