使用 R 中的 ggplot2 在圆环图中添加“0”小数
Adding "0" decimal in a donut chart using ggplot2 from R
我使用 ggplot2 包在 R 中创建了一个圆环图。现在我需要在“否”切片中添加“0”小数。它现在在图表中显示为“74%”——但我想将其显示为“74.0%”。以下是代码和图表-
df = data.frame(Dep = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"),
Percentage = c(74.0, 12.8, 9.4, 2.3, 1.6),
Count = c(284, 49, 36, 9, 6))
df$Dep = factor(df$Dep, levels = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"))
df = df %>%
arrange(desc(Dep)) %>%
mutate (Percentage) %>%
mutate (ypos = cumsum(Percentage)-0.5*Percentage)
donut= ggplot(df, aes(x =2, y=Percentage,fill=Dep))+
geom_bar(stat="identity")+
coord_polar("y", start=180)+
scale_fill_brewer(palette = "Set2")+
theme_void()+
geom_text(aes(y=ypos, label=paste0(round(Percentage,1),"%")),
color = "black", size=4.5, angle = 0)+
xlim(0.25, 2.5)+theme(legend.position=c(.5, .5))+
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(legend.title = element_text(size=18, face="bold",)) +
theme(legend.text = element_text(size = 14, face = "bold"))
donut
我得到了这个圆环图 -
我正在寻找这个解决方案,但找不到任何有用的东西。提前致谢。非常感谢您的帮助。
你只需要使用sprintf()
。请在下面找到一个代表。
Reprex
library(ggplot2)
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> Les objets suivants sont masqués depuis 'package:stats':
#>
#> filter, lag
#> Les objets suivants sont masqués depuis 'package:base':
#>
#> intersect, setdiff, setequal, union
df = data.frame(Depression = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"),
Percentage = c(74.0, 12.8, 9.4, 2.3, 1.6),
Count = c(284, 49, 36, 9, 6))
df$Depression = factor(df$Depression, levels = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"))
df = df %>%
arrange(desc(Depression)) %>%
mutate (Percentage) %>%
mutate (ypos = cumsum(Percentage)-0.5*Percentage)
donut= ggplot(df, aes(x =2, y=Percentage,fill=Depression))+
geom_bar(stat="identity")+
coord_polar("y", start=180)+
scale_fill_brewer(palette = "Pastel2")+
theme_void()+
geom_text(aes(y=ypos, label=paste0(sprintf("%.1f",Percentage),"%")),
color = "black", size=4.5, angle = 0)+
xlim(0.25, 2.5)+theme(legend.position=c(.5, .5))+
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(legend.title = element_text(size=18, face="bold",)) +
theme(legend.text = element_text(size = 14, face = "bold"))
donut
由 reprex package (v2.0.1)
创建于 2022-03-05
一种选择是使用 scales::percent
:
格式化您的百分比
library(ggplot2)
library(scales)
ggplot(df, aes(x = 2, y = Percentage, fill = Dep)) +
geom_bar(stat = "identity") +
coord_polar("y", start = 180) +
scale_fill_brewer(palette = "Set2") +
theme_void() +
geom_text(aes(y = ypos, label = scales::percent(Percentage, scale = 1, accuracy = .1)),
color = "black", size = 4.5, angle = 0
) +
xlim(0.25, 2.5) +
theme(legend.position = c(.5, .5)) +
theme(panel.grid = element_blank()) +
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(legend.title = element_text(size = 18, face = "bold", )) +
theme(legend.text = element_text(size = 14, face = "bold"))
我使用 ggplot2 包在 R 中创建了一个圆环图。现在我需要在“否”切片中添加“0”小数。它现在在图表中显示为“74%”——但我想将其显示为“74.0%”。以下是代码和图表-
df = data.frame(Dep = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"),
Percentage = c(74.0, 12.8, 9.4, 2.3, 1.6),
Count = c(284, 49, 36, 9, 6))
df$Dep = factor(df$Dep, levels = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"))
df = df %>%
arrange(desc(Dep)) %>%
mutate (Percentage) %>%
mutate (ypos = cumsum(Percentage)-0.5*Percentage)
donut= ggplot(df, aes(x =2, y=Percentage,fill=Dep))+
geom_bar(stat="identity")+
coord_polar("y", start=180)+
scale_fill_brewer(palette = "Set2")+
theme_void()+
geom_text(aes(y=ypos, label=paste0(round(Percentage,1),"%")),
color = "black", size=4.5, angle = 0)+
xlim(0.25, 2.5)+theme(legend.position=c(.5, .5))+
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(legend.title = element_text(size=18, face="bold",)) +
theme(legend.text = element_text(size = 14, face = "bold"))
donut
我得到了这个圆环图 -
我正在寻找这个解决方案,但找不到任何有用的东西。提前致谢。非常感谢您的帮助。
你只需要使用sprintf()
。请在下面找到一个代表。
Reprex
library(ggplot2)
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> Les objets suivants sont masqués depuis 'package:stats':
#>
#> filter, lag
#> Les objets suivants sont masqués depuis 'package:base':
#>
#> intersect, setdiff, setequal, union
df = data.frame(Depression = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"),
Percentage = c(74.0, 12.8, 9.4, 2.3, 1.6),
Count = c(284, 49, 36, 9, 6))
df$Depression = factor(df$Depression, levels = c("No",
"Mild",
"Moderate",
"Moderately Severe",
"Severe"))
df = df %>%
arrange(desc(Depression)) %>%
mutate (Percentage) %>%
mutate (ypos = cumsum(Percentage)-0.5*Percentage)
donut= ggplot(df, aes(x =2, y=Percentage,fill=Depression))+
geom_bar(stat="identity")+
coord_polar("y", start=180)+
scale_fill_brewer(palette = "Pastel2")+
theme_void()+
geom_text(aes(y=ypos, label=paste0(sprintf("%.1f",Percentage),"%")),
color = "black", size=4.5, angle = 0)+
xlim(0.25, 2.5)+theme(legend.position=c(.5, .5))+
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(legend.title = element_text(size=18, face="bold",)) +
theme(legend.text = element_text(size = 14, face = "bold"))
donut
由 reprex package (v2.0.1)
创建于 2022-03-05一种选择是使用 scales::percent
:
library(ggplot2)
library(scales)
ggplot(df, aes(x = 2, y = Percentage, fill = Dep)) +
geom_bar(stat = "identity") +
coord_polar("y", start = 180) +
scale_fill_brewer(palette = "Set2") +
theme_void() +
geom_text(aes(y = ypos, label = scales::percent(Percentage, scale = 1, accuracy = .1)),
color = "black", size = 4.5, angle = 0
) +
xlim(0.25, 2.5) +
theme(legend.position = c(.5, .5)) +
theme(panel.grid = element_blank()) +
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(legend.title = element_text(size = 18, face = "bold", )) +
theme(legend.text = element_text(size = 14, face = "bold"))