ggplot2 geom_text在饼图中的位置 [英] ggplot2 geom_text position in pie chart
本文介绍了ggplot2 geom_text在饼图中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用ggplot2绘制饼图,并成功将百分比标签置于每个切片的中心
库(dplyr)库(ggplot2)图书馆(ggpubr)库("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
(v0.3.0)创建于2020-06-03 sup>
I am plotting pie charts with ggplot2 and succeeded in having the percentage-labels centered in each slice
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
I try to place the percent-label outside the pie for better readability
Any recommendation?
Thank you
解决方案
This can be achieved by setting the x
aesthetic inside geom_text
, e.g. x = 1.6
will put the label just outside of the pie.
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()
Created on 2020-06-03 by the reprex package (v0.3.0)
这篇关于ggplot2 geom_text在饼图中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文