饼图 - 如何在正确的位置获取百分比文本?
Pie chart - How to get the percent text at the right location?
我无法在饼图的正确位置显示百分比信息。有人可以非常友好地帮助我吗?非常感谢!
#sample dataframe
d <- data.frame(facet=c('a','b','c', "d"), value=c('0.46','0.11','0.18', "0.25"))
d$value <- as.numeric(as.character(d$value))
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
d$perc <- round(d$value/sum(d$value) * 100,0)
d$pos <- cumsum(d$perc) - sapply(d$perc,function(x) cumsum(x)-0.5*x)
bp <- ggplot(data=d, aes(x="", y=perc, fill=facet))+
geom_bar(width = 1, stat = "identity") +
geom_text(aes(x="", y=pos, label=paste0(perc,"%"))) +
#geom_text(aes(x="", y=value/4+ c(0, cumsum(value)[-length(value)]), label=percent(value/100)))
scale_fill_manual(values = c("a" = "#b2df8a", "b" = "#238b45", "c" = "#636363", "d"="orange"))
bp
pie <- bp + coord_polar("y", start=0) + blank_theme +
theme(axis.text.x=element_blank())
pie
这可以通过 ggpiestats
函数直接实现。它只需要对您的数据框进行轻微修改-
library(ggstatsplot)
set.seed(123)
# data
d <-
data.frame(
facet = c('a', 'b', 'c', "d"),
value = c(46, 11, 18, 25)
)
# plot with statistical details in the subtitle
ggstatsplot::ggpiestats(data = d,
main = facet,
counts = value)
如果你不想要统计测试细节,想进一步自定义剧情的美感,你也可以使用ggplot2
函数-
# customizing it further
# change the slice label
ggstatsplot::ggpiestats(data = d,
main = facet,
counts = value,
slice.label = "both",
package = "wesanderson",
palette = "Royal2") +
ggplot2::labs(subtitle = NULL)
由 reprex package (v0.2.1)
创建于 2019-02-09
碰巧由于某种原因ggplot2
在处理标签时走向了另一个方向。因此,而不是使用
d$pos <- 100 - (cumsum(d$perc) - sapply(d$perc, function(x) cumsum(x) - 0.5 * x))
给予
我无法在饼图的正确位置显示百分比信息。有人可以非常友好地帮助我吗?非常感谢!
#sample dataframe
d <- data.frame(facet=c('a','b','c', "d"), value=c('0.46','0.11','0.18', "0.25"))
d$value <- as.numeric(as.character(d$value))
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
d$perc <- round(d$value/sum(d$value) * 100,0)
d$pos <- cumsum(d$perc) - sapply(d$perc,function(x) cumsum(x)-0.5*x)
bp <- ggplot(data=d, aes(x="", y=perc, fill=facet))+
geom_bar(width = 1, stat = "identity") +
geom_text(aes(x="", y=pos, label=paste0(perc,"%"))) +
#geom_text(aes(x="", y=value/4+ c(0, cumsum(value)[-length(value)]), label=percent(value/100)))
scale_fill_manual(values = c("a" = "#b2df8a", "b" = "#238b45", "c" = "#636363", "d"="orange"))
bp
pie <- bp + coord_polar("y", start=0) + blank_theme +
theme(axis.text.x=element_blank())
pie
这可以通过 ggpiestats
函数直接实现。它只需要对您的数据框进行轻微修改-
library(ggstatsplot)
set.seed(123)
# data
d <-
data.frame(
facet = c('a', 'b', 'c', "d"),
value = c(46, 11, 18, 25)
)
# plot with statistical details in the subtitle
ggstatsplot::ggpiestats(data = d,
main = facet,
counts = value)
如果你不想要统计测试细节,想进一步自定义剧情的美感,你也可以使用ggplot2
函数-
# customizing it further
# change the slice label
ggstatsplot::ggpiestats(data = d,
main = facet,
counts = value,
slice.label = "both",
package = "wesanderson",
palette = "Royal2") +
ggplot2::labs(subtitle = NULL)
由 reprex package (v0.2.1)
创建于 2019-02-09碰巧由于某种原因ggplot2
在处理标签时走向了另一个方向。因此,而不是使用
d$pos <- 100 - (cumsum(d$perc) - sapply(d$perc, function(x) cumsum(x) - 0.5 * x))
给予