如何将图例值显示为百分比

Ada*_*min 3 r ggplot2

对于以下数据:

   RW GA Freq   percFreq
    0  0    9 0.13043478
    0  3    1 0.01449275
    0 14    1 0.01449275
    0 16    1 0.01449275
    0 23    1 0.01449275
    0 25    1 0.01449275
    0 29    2 0.02898551
    0 30    1 0.01449275
    2 30    1 0.01449275
   15 30    2 0.02898551
   19 30    1 0.01449275
   22 30    1 0.01449275
   24 30    1 0.01449275
   29 30    1 0.01449275
   30 29   16 0.23188406
   30 30   29 0.42028986
Run Code Online (Sandbox Code Playgroud)

我想更改下图中的图例值以百分比显示:

在此输入图像描述

生成绘图的脚本是:

ggplot(counts, aes(x=RW, y=GA, size=Freq, color=as.factor(percFreq))) + geom_point(alpha=0.7) +
    scale_size(range = c(1, 10), name="Freq", limits=c(1,30), breaks=lbreaks) +
    scale_color_discrete(name="Freq", breaks=lbreaks)
Run Code Online (Sandbox Code Playgroud)

基本上,我希望将其显示为 42%,而不是在图例中显示 0.42028986。

我怎样才能做到这一点?

Lou*_*uis 5

使用“比例”库中的“百分比”。

加载scales库:

library(scales)
Run Code Online (Sandbox Code Playgroud)

并添加labels = percent到您的离散规模:

ggplot(counts, aes(x=RW, y=GA, size=Freq, color=as.factor(percFreq))) + 
  geom_point(alpha=0.7) +
  scale_size(range = c(1, 10), name="Freq", limits=c(1,30), breaks=lbreaks) +
  scale_color_discrete(name="Freq", breaks=lbreaks, labels = percent(lbreaks, accuracy = .01))
Run Code Online (Sandbox Code Playgroud)

如果您想更改数字的舍入方式,请使用accuracy参数:

scales::percent(percFreq, accuracy = .001)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(这有accuracy = .1

希望这可以帮助。