热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Rggplot2画饼状图

library(ggplot2)type<-c(A,B,C,D,E,F,G)nums<-c(10,23,8,33,12,40,60)

library(ggplot2)
type <- c('A','B','C','D','E','F','G')
nums <- c(10,23,8,33,12,40,60)
df <- data.frame(type = type, nums = nums)

#绘制条形图
p <- ggplot(data = df, mapping = aes(x = 'Content', y = nums, fill = type)) + geom_bar(stat = 'identity', position = 'stack', width = 1)
p

label_value <- paste('(', round(df$nums/sum(df$nums) * 100, 1), '%)', sep = '')
label_value
label <- paste(df$type, label_value, sep = '')
label
p + coord_polar(theta = 'y') + labs(x = '', y = '', title = '') + theme(axis.text = element_blank()) + theme(axis.ticks = element_blank()) + theme(legend.position = "none") + geom_text(aes(y = df$nums/2 + c(0, cumsum(df$nums)[-length(df$nums)]), x = sum(df$nums)/150, label = label)) 


library(ggplot2)
#type <- c('A','B','C','D','E','F','G')
#nums <- c(10,23,8,33,12,40,60)
type <- c('1 HBase','2 Spark','3 Kylin','4 Data dev&analysis')
nums <- c(85,5,5,5)
df <- data.frame(type = type, nums = nums)

#绘制条形图
p <- ggplot(data = df, mapping = aes(x = 'Content', y = nums, fill = type)) + geom_bar(stat = 'identity', position = 'stack', width = 1)
p

label_value <- paste('(', round(df$nums/sum(df$nums) * 100, 1), '%)', sep = '')
label_value
label <- paste(df$type, label_value, sep = '')
label
p + coord_polar(theta = 'y') + labs(x = '', y = '', title = '') + theme(axis.text = element_blank()) + theme(axis.ticks = element_blank()) + scale_fill_discrete(labels = label)


推荐阅读
author-avatar
茨冈人686
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有