在R堆叠条形图标签中减少小数点后面的数字。

huangapple go评论60阅读模式
英文:

Reducing numbers behind comma in R stacked barchart label

问题

我计算了以下数据以在R中绘制堆叠柱状图的标签:

        Scenario Losses      Model   percent
    1 Scenario 1  23221   Upstream 0.6095231
    2 Scenario 1  14876 Downstream 0.3904769
    3 Scenario 2    722   Upstream 0.3764338
    4 Scenario 2   1196 Downstream 0.6235662
    5 Scenario 3  28487   Upstream 0.7256355
    6 Scenario 3  10771 Downstream 0.2743645

当我绘制这个图表:

ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(percent * 100, "%", sep = ""))) + 
  geom_bar(stat = "identity", position = "fill", color = "black", alpha = 0.8) +
  theme_bw() +
  scale_fill_manual(values = c("#00008B", "#800080")) + 
  geom_text(position = position_stack(vjust = 0.5), size = 2)

得到的图形是:

在R堆叠条形图标签中减少小数点后面的数字。

要解决这个问题,广泛讨论的解决方案是添加以下代码以减少逗号的数量:

scale_y_continuous(label = scales::percent(percent, accuracy = 0.1))

但我尝试了许多解决方法,每次都会出现以下错误:

Error in number(x = x, accuracy = accuracy, scale = scale, prefix = prefix,  : 
  object 'percent' not found

我不太明白需要改变什么,因为我在另一个讨论中精确地看到这段代码成功运行。感激任何帮助。

英文:

I computed the following data in order to plot labels to my stacked barchart in R:

    Scenario Losses      Model   percent
1 Scenario 1  23221   Upstream 0.6095231
2 Scenario 1  14876 Downstream 0.3904769
3 Scenario 2    722   Upstream 0.3764338
4 Scenario 2   1196 Downstream 0.6235662
5 Scenario 3  28487   Upstream 0.7256355
6 Scenario 3  10771 Downstream 0.2743645

When I plot this:

ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(percent * 100, "%", sep = ""))) + 
  geom_bar(stat = "identity", position = "fill", color = "black", alpha=0.8)+
  theme_bw()+
  scale_fill_manual(values = c("#00008B", "#800080"))+ 
  geom_text(position = position_stack(vjust = 0.5), size = 2)

The resulting graph is:

在R堆叠条形图标签中减少小数点后面的数字。

The solution to this has been widely discussed online. By adding the following code, the number of commas ought to be reduced:

  scale_y_continuous(label = scales::percent(percent, accuracy=0.1))

I've tried a number of workarounds but everytime I end up with:

Error in number(x = x, accuracy = accuracy, scale = scale, prefix = prefix,  : 
  object 'percent' not found

I can't really fanthom what I need to chance since I saw precisely this code chunk being run succesfully on another thread.

Any help is appreciated.

答案1

得分: 3

你可以使用round函数来将你的label四舍五入,例如只显示两位小数,像这样:

library(ggplot2)
ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(round(percent * 100, 2), ""%"", sep = """"))) + 
  geom_bar(stat = ""identity"", position = ""fill"", color = ""black"", alpha=0.8)+
  theme_bw()+
  scale_fill_manual(values = c(""#00008B"", ""#800080"))+ 
  geom_text(position = position_stack(vjust = 0.5), size = 2)

在R堆叠条形图标签中减少小数点后面的数字。<!-- -->

<sup>Created on 2023-03-31 with reprex v2.0.2</sup>


英文:

You could use round to your label to show for example only two digits like this:

library(ggplot2)
ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(round(percent * 100, 2), &quot;%&quot;, sep = &quot;&quot;))) + 
  geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;, color = &quot;black&quot;, alpha=0.8)+
  theme_bw()+
  scale_fill_manual(values = c(&quot;#00008B&quot;, &quot;#800080&quot;))+ 
  geom_text(position = position_stack(vjust = 0.5), size = 2)

在R堆叠条形图标签中减少小数点后面的数字。<!-- -->

<sup>Created on 2023-03-31 with reprex v2.0.2</sup>


Data used:

test = read.table(text = &quot;    Scenario Losses      Model   percent
1 Scenario_1  23221   Upstream 0.6095231
2 Scenario_1  14876 Downstream 0.3904769
3 Scenario_2    722   Upstream 0.3764338
4 Scenario_2   1196 Downstream 0.6235662
5 Scenario_3  28487   Upstream 0.7256355
6 Scenario_3  10771 Downstream 0.2743645&quot;, header = TRUE)

答案2

得分: 2

不使用paste,而是使用如下所示的sprintf

label = sprintf("%.2f%%", percent * 100)

例如:

ggplot(test, aes(x = Scenario, y = percent, fill = Model,
                 label = sprintf("%.2f%%", percent * 100))) + 
  geom_bar(stat = "identity", position = "fill", color = "black", alpha=0.8) +
  theme_bw() +
  scale_fill_manual(values = c("#00008B", "#800080")) + 
  geom_text(position = position_stack(vjust = 0.5), size = 2)
英文:

Instead of using paste, use sprintf as shown below:

 label = sprintf(&quot;%.2f%%&quot;,percent * 100)

ie

ggplot(test, aes(x = Scenario, y = percent, fill = Model,
                 label = sprintf(&quot;%.2f%%&quot;,percent * 100))) + 
  geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;, color = &quot;black&quot;, alpha=0.8)+
  theme_bw()+
  scale_fill_manual(values = c(&quot;#00008B&quot;, &quot;#800080&quot;))+ 
  geom_text(position = position_stack(vjust = 0.5), size = 2)

答案3

得分: 2

另一种方法是同时使用scales::percent来处理文本标签和坐标轴标签,如下所示:

注意:数据无耻地从@Quinten那里偷了来。

library(ggplot2)

ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = scales::percent(percent))) +
  geom_bar(stat = "identity", position = "fill", color = "black", alpha = 0.8) +
  theme_bw() +
  scale_fill_manual(values = c("#00008B", "#800080")) +
  scale_y_continuous(labels = scales::percent) +
  geom_text(position = position_stack(vjust = 0.5))

在R堆叠条形图标签中减少小数点后面的数字。

英文:

And yet another approach would be to use scales::percent for both the text labels and the axis labels like so:

Note: Shameless stolen the data from @Quinten.

library(ggplot2)

ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = scales::percent(percent))) +
  geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;, color = &quot;black&quot;, alpha = 0.8) +
  theme_bw() +
  scale_fill_manual(values = c(&quot;#00008B&quot;, &quot;#800080&quot;)) +
  scale_y_continuous(labels = scales::percent) +
  geom_text(position = position_stack(vjust = 0.5))

在R堆叠条形图标签中减少小数点后面的数字。

huangapple
  • 本文由 发表于 2023年3月31日 17:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定