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

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

Reducing numbers behind comma in R stacked barchart label

问题

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

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

当我绘制这个图表:

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

得到的图形是:

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

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

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

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

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

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

英文:

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

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

When I plot this:

  1. ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(percent * 100, "%", sep = ""))) +
  2. geom_bar(stat = "identity", position = "fill", color = "black", alpha=0.8)+
  3. theme_bw()+
  4. scale_fill_manual(values = c("#00008B", "#800080"))+
  5. 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:

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

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

  1. Error in number(x = x, accuracy = accuracy, scale = scale, prefix = prefix, :
  2. 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四舍五入,例如只显示两位小数,像这样:

  1. library(ggplot2)
  2. ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(round(percent * 100, 2), ""%"", sep = """"))) +
  3. geom_bar(stat = ""identity"", position = ""fill"", color = ""black"", alpha=0.8)+
  4. theme_bw()+
  5. scale_fill_manual(values = c(""#00008B"", ""#800080"))+
  6. 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:

  1. library(ggplot2)
  2. ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = paste(round(percent * 100, 2), &quot;%&quot;, sep = &quot;&quot;))) +
  3. geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;, color = &quot;black&quot;, alpha=0.8)+
  4. theme_bw()+
  5. scale_fill_manual(values = c(&quot;#00008B&quot;, &quot;#800080&quot;))+
  6. 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:

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

答案2

得分: 2

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

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

例如:

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

Instead of using paste, use sprintf as shown below:

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

ie

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

答案3

得分: 2

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

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

  1. library(ggplot2)
  2. ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = scales::percent(percent))) +
  3. geom_bar(stat = "identity", position = "fill", color = "black", alpha = 0.8) +
  4. theme_bw() +
  5. scale_fill_manual(values = c("#00008B", "#800080")) +
  6. scale_y_continuous(labels = scales::percent) +
  7. 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.

  1. library(ggplot2)
  2. ggplot(test, aes(x = Scenario, y = percent, fill = Model, label = scales::percent(percent))) +
  3. geom_bar(stat = &quot;identity&quot;, position = &quot;fill&quot;, color = &quot;black&quot;, alpha = 0.8) +
  4. theme_bw() +
  5. scale_fill_manual(values = c(&quot;#00008B&quot;, &quot;#800080&quot;)) +
  6. scale_y_continuous(labels = scales::percent) +
  7. 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:

确定