W11YA

2019-03-10   阅读量: 896

R语言

用ggplot绘制饼图

扫码加入数据分析学习群

library(dplyr)

library(ggplot2)

library(ggmap) # 为了引用主题theme_nothing,用来消除原始ggplot绘图自带的一切标签

df <- data.frame(value = c(52, 239, 9),

Group = c("Positive", "Negative", "Neutral")) %>%

# factor levels need to be the opposite order of the cumulative sum of the values

mutate(Group = factor(Group, levels = c("Neutral", "Negative", "Positive")),

cumulative = cumsum(value),

midpoint = cumulative - value / 2,

label = paste0(Group, " ", round(value / sum(value) * 100, 1), "%"))

ggplot(df, aes(x = 1, weight = value, fill = Group)) +

geom_bar(width = 1, position = "stack") +

coord_polar(theta = "y") ## 以y轴建立极坐标

+

geom_text(aes(x = 1.3, y = midpoint, label = label)) ## 加上百分比标签的位置和数值

+

theme_nothing()

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
27.2058 1 4 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子