ggplot2 geom_text在饼图中的位置。

问题描述 投票:0回答:1

我正在用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

饼图

我试着把百分比标签放在饼的外面,以提高可读性。

有什么推荐吗?

谢谢你的推荐

ggplot2 pie-chart geom-text
1个回答
1
投票

这可以通过设置 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()

创建于2020-06-03,由 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.