r - 将图例添加到由 ggplot 包装的饼图
问题描述
我对 R 很陌生,需要一些支持来使用 pie 函数。
我需要返回一个 ggplot,所以我用这个包装了 pie 函数。输出是预期的饼图,唯一的问题是,我还想在它旁边有一个图例。
我需要返回一个 ggplot 但不能将它与图例函数结合起来。有人可以提供一些指导吗
解决方案
你可以尝试这样的事情:
library(ggplot2)
library(dplyr)
#Data
data <- data.frame(slices = c(10, 12, 4, 5, 8),
countries = c("US", "Japan", "UK", "Germany", "France"),
stringsAsFactors = F)
#Create variable
data <- data %>%
mutate(per=slices/sum(slices)) %>%
arrange(desc(countries))
data$label <- scales::percent(data$per)
#Plot
ggplot(data=data)+
geom_bar(aes(x="", y=per, fill=countries), stat="identity", width = 1)+
coord_polar("y", start=0)+
theme_void()+
geom_text(aes(x=1, y = cumsum(per) - per/2, label=label))+
ggtitle("pie chart example")
推荐阅读
- java - 无法在 selenium 中打开浏览器,抛出“null”消息
- python - 在Django中创建成功时如何返回创建的数据
- node.js - 使用 nvm 更新节点的系统版本
- c# - C#.net winforms:无法将验证码存储到图片框中
- macos - OS X 下的 GLX 手册页
- dart - 由脚手架 floatingActionButton 更新的小部件状态
- sql - 如何优化我的查询?
- html - 无法使用 301 和 .htacces 重定向
- ajax - 由于设置了自定义标头,ajax http 自动更改为 http 帖子
- ssl - 从 .cer 和 pfx 的密码生成 pfx