ggplot2 - ggplot2 geom_text 在饼图中的位置
问题描述
我正在用 ggplot2 绘制饼图,并成功地将百分比标签置于每个切片的中心
library(dplyr)
library(ggplot2)
library(ggpubr)
library("readxl")
df <- read_excel("Radiocomp.xlsx")
df$Pattern <- factor(cc$Pattern)
str(cc)
GGO <- ggplot(data=df, aes(x = "", y = GGO, fill = Pattern)) +
geom_bar(stat="identity", color = "white") +
geom_text(aes(label = paste0(GGO, "%")), position = position_stack(vjust = 0.5)) +
coord_polar("y") +
theme_void()
GGO
我尝试将百分比标签放在饼图之外以提高可读性
有什么推荐吗?
谢谢
解决方案
这可以通过将x
美学设置在内部来实现geom_text
,例如x = 1.6
将标签放在派的外面。
library(ggplot2)
library(dplyr)
# example data
mpg1 <- mpg %>%
count(class) %>%
mutate(pct = n / sum(n))
ggplot(mpg1, aes(x = "", y = pct, fill = class)) +
geom_bar(stat = "identity", color = "white") +
geom_text(aes(x = 1.6, label = scales::percent(pct, accuracy = .1)), position = position_stack(vjust = .5)) +
coord_polar("y") +
theme_void()
由reprex 包(v0.3.0)于 2020-06-03 创建
推荐阅读
- excel - 在excel VBA中,如何根据另一列中的值变化将一列中的数字增加一
- c++ - 为什么在多文件中使用类声明朋友功能时出现错误类未定义?
- c++ - 无法在结构中定义 3D 向量
- c# - Linux上的ASP.NET Core 2.2项目找不到卫星资源
- java - Google Directory API 仅针对 ADMIN_DIRECTORY_ORGUNIT 提供带有服务帐户的 401
- powershell - 如何从 PowerShell 中的可执行文件中读取标准输出(UTF-8)作为字符串?
- android - OSX - Android Emulator 由于内部错误而关闭:
- dictionary - 带有 tmap 的构面图
- amazon-web-services - 如何使用文件夹的最后修改日期将文件上传到 S3?
- sql - Presto:从可变长度数组中删除最后一个元素