热线电话:13121318867

登录
2019-03-10 阅读量: 1318
用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()

27.2058
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子