英文:
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)
得到的图形是:
要解决这个问题,广泛讨论的解决方案是添加以下代码以减少逗号的数量:
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:
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)
<!-- -->
<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), "%", 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)
<!-- -->
<sup>Created on 2023-03-31 with reprex v2.0.2</sup>
Data used:
test = read.table(text = " 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", 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("%.2f%%",percent * 100)
ie
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)
答案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))
英文:
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 = "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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论