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

R 如何将图例值显示为百分比,r,ggplot2,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.0

有关以下数据:

   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
我想将以下绘图中的图例值更改为百分比:

生成绘图的脚本是:

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)
基本上,我不希望在图例中显示0.42028986,而是希望它显示为42%


我该怎么做呢?

您可以将
percFreq
转换为百分比

df$percFreq <- df$percFreq*100 
我建议不要,但如果您想要
%
和数字,只需
粘贴(df$perfreq,“%”,sep=“”)


您可以将
percFreq
转换为百分比

df$percFreq <- df$percFreq*100 
我建议不要,但如果您想要
%
和数字,只需
粘贴(df$perfreq,“%”,sep=“”)

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

library(scales)
并将
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))
如果要更改数字的舍入方式,请使用
accurity
参数:

scales::percent(percFreq, accuracy = .001)

(这具有
精度=.1

希望这有帮助。

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

library(scales)
并将
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))
如果要更改数字的舍入方式,请使用
accurity
参数:

scales::percent(percFreq, accuracy = .001)

(这具有
精度=.1


希望这有帮助。

谢谢。我在abs(x)中得到了这个错误
错误:数学函数的非数值参数
谢谢。我查过你的鳕鱼,但传说消失了。你知道是什么情况吗?你能提供
lbreaks
的值吗?我想这和这个变量有关谢谢。我在abs(x)中得到了这个错误
错误:数学函数的非数值参数
谢谢。我查过你的鳕鱼,但传说消失了。你知道是什么情况吗?你能提供
lbreaks
的值吗?我想这和这个变量有关谢谢。不显示42.028986,如何将其显示为42.02%?
round(df$perfreq,digits=2)
谢谢。不显示42.028986,如何将其显示为42.02%?
round(df$perfreq,digits=2)