繁体   English   中英

R语言单列饼图

[英]Pie chart of single column in R Language

如何用R语言创建单列的饼图? 尝试在 google 上搜索它,但我找不到与单个列相关的任何内容。

假设您的数据框如下所示:

df <- data.frame(single_column = c(21, 48, 15, 16))

df
#>   single_column
#> 1            21
#> 2            48
#> 3            15
#> 4            16

然后你可以这样做:

library(ggplot2)

ggplot(df, aes(x = 1, y = single_column, fill = factor(single_column))) + 
  geom_col() +
  geom_text(position = position_stack(vjust = 0.5), 
            aes(label = single_column)) +
  coord_polar(theta = "y") +
  theme_void() +
  theme(legend.position = "none")

在此处输入图片说明

这来自 Quick-R 的网站:

# Pie Chart with Percentages
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)),
   main="Pie Chart of Countries")

希望它适合您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM