Move labels in ggplot2 pie graph

守給你的承諾、 提交于 2021-02-11 10:33:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/58102321/move-labels-in-ggplot2-pie-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!