如何在 R 中使用 ggplot2 显示带有欧元符号的 Y 轴?

当我们在 R 数据框中有一个欧元货币列作为响应变量时,我们可能会在使用 ggplot2 包创建的图中显示欧元符号。为此,我们可以使用 scales 包,Y 轴的比例将通过使用命令 scale_y_continuous(labels=dollar_format(suffix="€",prefix="")) 到 plot 命令来更改。

考虑以下数据框 -

示例

x<-rpois(20,5)
y<-rpois(20,100)
df<-data.frame(x,y)
df
输出结果
   x   y
1  8   112
2  10  81
3  4   97
4  7   104
5  4   98
6  5   99
7  5   97
8  7   97
9  5   95
10 3   98
11 6   92
12 6   80
13 6   92
14 6  108
15 7  113
16 4  103
17 3  106
18 2  116
19 4  105
20 6  83

加载 ggplot2 包并在 x 和 y 之间创建散点图 -

示例

library(ggplot2)
ggplot(df,aes(x,y))+geom_point()
输出结果

加载刻度包并创建带有 Y 轴标签的欧元符号的散点图 -

示例

library(scales)
ggplot(df)+geom_point(aes(x,y))+scale_y_continuous(labels=dollar_format(suffix="€",prefix=""))
输出结果