在ggplot2饼图中移动标签 [英] Move labels in ggplot2 pie graph
问题描述
我从
因此,在此示例中,1%和5%将位于灰色区域.
这绝非优雅,但它可以提供您所需要的东西.这种方法涉及计算标签的位置(对于 y
是 value
的中点),并使用不同的 x
位置和 nudge_x
用于带段的外部标签.也许这会给您一些想法吗?
library(ggrepel)库(ggplot2)图书馆(dplyr)图书馆(秤)库(重塑)y<-data.frame(状态= c("AR"),ac = c(0.43),人= c(0.26),ltc = c(0.25),护理= c(0.05),dsh = c(0.01))y2< -melt(y,id.var ="state")阈值<-.07y2--y2%>%mutate(cs = rev(cumsum(rev(value))),ypos =值/2 + lead(cs,1),ypos = ifelse(is.na(ypos),value/2,ypos),xpos = ifelse(值>阈值,1,1.3),xn = ifelse(值>阈值,0,.5))测试<-ggplot(y2,aes(x = 1,y = value,fill = variable))+geom_bar(width = 1,stat ="identity")+geom_text_repel(aes(label = paste(y2 $ variable,percent(value)),x = xpos,y = ypos),color ="white",size = 5,nudge_x = y2 $ xn,segment.size = .5)+coord_polar("y",start = 0)+scale_fill_manual(values = c(#003C64",#0077C8",#7FBBE3",#BFDDF1",#00BC87"))+主题(axis.text = element_blank(),axis.ticks = element_blank(),panel.grid = element_blank())测试
I know from How to avoid label overlap in pie chart that I can use ggrepel
to make labels not overlap in a pie graph. I would like percentages less than 7% moved to the outside and numbers 7% or more on top of their slice of the pie. Any ideas?
library( ggrepel )
library( ggplot2)
library( dplyr)
library( scales )
library( reshape )
y <- data.frame(
state = c( "AR" ) ,
ac = c( 0.43 ) ,
man = c( 0.26 ) ,
ltc = c( 0.25 ) ,
care = c( 0.05 ) ,
dsh = c( 0.01 )
)
y2 <- melt( y , id.var="state" )
test <- ggplot( y2 , aes( x=1 , y=value , fill=variable )) +
geom_bar( width = 1 , stat = "identity" ) +
geom_text_repel( aes( label = paste( y2$variable , percent( value )) ) , position = position_fill( vjust = 0.5 ) , color="white" , size=5 ) +
coord_polar( "y" , start = 0 ) +
scale_fill_manual( values=c( "#003C64" , "#0077C8" , "#7FBBE3" , "#BFDDF1" , "#00BC87" ) )
test
So in this example, the 1% and 5% would be in the grey area.
This is by no means elegant, but it may provide what you are looking for. This approach involves computing the locations for labels (midpoints in value
for y
), and using different x
positions and nudge_x
for the outside labels with segments. Maybe this will give you some ideas to work with?
library( ggrepel )
library( ggplot2)
library( dplyr)
library( scales )
library( reshape )
y <- data.frame(
state = c( "AR" ) ,
ac = c( 0.43 ) ,
man = c( 0.26 ) ,
ltc = c( 0.25 ) ,
care = c( 0.05 ) ,
dsh = c( 0.01 )
)
y2 <- melt( y , id.var="state" )
threshold <- .07
y2 <- y2 %>%
mutate(cs = rev(cumsum(rev(value))),
ypos = value/2 + lead(cs, 1),
ypos = ifelse(is.na(ypos), value/2, ypos),
xpos = ifelse(value > threshold, 1, 1.3),
xn = ifelse(value > threshold, 0, .5))
test <- ggplot( y2 , aes( x=1 , y=value , fill=variable )) +
geom_bar( width = 1 , stat = "identity" ) +
geom_text_repel( aes( label = paste( y2$variable , percent( value )), x = xpos, y = ypos ) ,
color="white" , size=5, nudge_x = y2$xn, segment.size = .5 ) +
coord_polar( "y" , start = 0 ) +
scale_fill_manual( values=c( "#003C64" , "#0077C8" , "#7FBBE3" , "#BFDDF1" , "#00BC87" ) ) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
test
这篇关于在ggplot2饼图中移动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!