Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

r - Changing the radius of a coord_polar ggplot

I'm looking to reduce the display radius of a pie chart built in ggplot2 relative to the rest of the plot (as the defaults keep cutting off my category labels).

Here's some dummy data and code that should show you what I'm experiencing:

library(ggplot2)
library(scales)
library(grid)

Region <- c("North America", "Central America", "South America", "Carribbean",
            "Western Africa", "Northern Africa", "Southern Afica", "Eastern Africa")
Conti <- c(rep("Americas",4), rep("Africa",4))
Freq <- c(runif(8, 1, 100))
Pct <- c(Freq/sum(Freq))
Pos <- c(cumsum(360*Pct)-(360*Pct/2))
Pos <- c(ifelse(Pos<=180,Pos,Pos-180))
df <- data.frame(Region, Conti, Freq, Pct, Pos)

pl <- ggplot(df, aes(x="", y=Freq, fill=Conti)) +
  geom_bar(stat="identity", color="black", width=1) +
  coord_polar(theta='y') +
  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_text(color='black', size=18, angle=90-df$Pos),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.margin = unit(0, "lines"),
        plot.background = element_rect(fill = "white"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        legend.position = "none") +
  scale_y_continuous(
    breaks=cumsum(df$Freq) - df$Freq/2,
    labels=paste0(df$Region," ",percent(df$Pct)))

print(pl)

If I reduce the size of the labels they become illegible relative to the pie and if I increase them they get cut-off. No matter how I try to adjust the aes(), limits, panel.margin etc. to get the balance right ggplot2 keeps automatically re-sizing the pie to occupy the same radii.

Ideally, I'd like to shrink the pie by half to allow more room for labels.

I appreciate that this isn't the prettiest plot, however, I'm updating an old figure so need to maintain the format for comparison. Any suggestions would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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='
'),
                "
(", percent(df$Pct), ")"))
ggsave('pie.png', plot=pl, height=15, width=15)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...