Changing the radius of a coord_polar ggplot

為{幸葍}努か 提交于 2019-11-29 10:41:32

My suggestion would be to use line breaks.

pl <- pl + scale_y_continuous(
  breaks=cumsum(df$Freq) - df$Freq/2,
  labels=paste0(sapply(strsplit(as.character(df$Region), " "), paste, collapse='\n'),
                "\n(", percent(df$Pct), ")"))
ggsave('pie.png', plot=pl, height=15, width=15)

Some months later I've found that the answer lies in using a numeric dummy value for X (rather than a null value "") and then adding limits that are larger than x to the dummy x-axis. Code as below.

The issue then becomes adjusting the axis labels in to align with the new radius as hjust= and vjust= don't seem to work with coord_polar().

To do this I've added the labels as geom_text() and removed the automatic labels. This now does what I need.

The key changes are in the top and bottom lines.

pl <- ggplot(df, aes(x=0.8, y=Freq, fill=Conti)) +
  geom_bar(stat="identity", color="black", width=1) +
  coord_polar(theta='y') +
  geom_text(aes(x=1.65, y=cumsum(df$Freq) - df$Freq/2,
            label=paste0(df$Region," ",percent(df$Pct)),
            angle=90-df$Pos)) +
  guides(fill=guide_legend(override.aes=list(colour=NA))) +
  theme(axis.line = element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.background = element_rect(fill = "white"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        legend.position = "none") +
  scale_x_discrete(limits=c(0, 1))

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